0

I have the array with ids

const [selected, setSelected] = React.useState<readonly string[]>([]);
var selectedId = props.selectedId;  

When clicking on the clean button I have added a useState() which check the given selectedId has a value or not If selectedId has a value then add a value in array, if not then remove all the elements from the array

useEffect(() => {
        if (selectedId != undefined) {  
            // Add selectedId in selectedIdsList
            setSelected(selectedId)
        } else {
            // remove all elements from setSelected
        }
    }, [selectedId]);

How to delete all elements from array

3
  • selectedIdsList isn't array it is object Commented Oct 19, 2022 at 13:05
  • @VishalBeep i have updated the code please check Commented Oct 19, 2022 at 13:07
  • 1
    to delete or empty an array do something like this selectedIdsList = [] but since you are using state you can set it something like this = setSelected([]) Commented Oct 19, 2022 at 13:08

3 Answers 3

2

To delete all elements inside array or empty an array we can do something like this selectedIdsList = [] but since you are using state you can set it something like this

 setSelected([]);
Sign up to request clarification or add additional context in comments.

Comments

0

You can reassign the array to an empty array selectedIdsList = []

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

you should write setSelected([])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.