10

I am rendering an array in a modal. Once the modal closes I need to empty the array.The following code updates the array but not clear array on click of closeModal.

constructor(props,context) {
   super(props,context);
   this.state = {
      myArray: []
   };

 }

 pushData(newVar) {
   this.setState((state) => {
       myArray: state.myArray.push(newVar)
   });
 }

 closeModal() {
   this.setState({
       myArray: []
   })
 }
7
  • 1
    Something like this.state.myarray=[] Commented Feb 17, 2016 at 20:09
  • Hey Sorry you are right @Antonio this.state.myarray=[] fixed my problem. Commented Feb 17, 2016 at 20:18
  • Push data seems weird... I would write this.state.myArray.push(newVar) Commented Feb 17, 2016 at 20:19
  • this.state.myArray.push(newVar) this way of pushing didnt work for me. I tried .concat too but didnt work Commented Feb 17, 2016 at 20:20
  • 1
    state.myArray.push(newVar) will not behave as expected either. Array.prototype.push returns the length of the new array, not the array with the new value in it. setState(state => ({myArray: state.myArray.concat(newVar)})) is closer to what you're looking for. Commented Feb 18, 2016 at 8:12

5 Answers 5

20

I found the problem is my closeModal didn’t get called at all on closing modal. I am doing that to closeModal on componentWillUnmount function. I understood that the below code causes problem.

this.state.myArray=[] // class component
const[myArray, setMyArray]=useState([]) // functional component

I changed it back to

this.setState({myArray: []}); // class component
setMyArray([]); // functional component
Sign up to request clarification or add additional context in comments.

2 Comments

Never assign to this.state outside of the constructor. That might fix your immediate problem but it will cause bugs.
Yes, use this.setState({ myArray: [] }); instead.
5

You can as well use this to clear array without using setState:

   this.state.your_array.length = 0;

This will work in any function.

Update Functional component:

   setYourArray([])

Comments

1

A couple of solutions with explanations to this (albeit in ES5) can be found here:

https://stackoverflow.com/a/29994490/4572987

2 Comments

So the pushData method correctly adds newVar into the state and causes a re-render correct? why not make those methods uniform and have closeModal look like: this.setState((state) => { myArray: [] });
If that doesn't work, I suspect it has something to do with the code changing the actual reference React has to the myArray variable, and as a result not affecting it at all..
1
this.state.array.splice();

This will Delete or truncate whole array

1 Comment

This will not update the state, also splice needs two params: 1. start The zero-based location in the array from which to start removing 2. deleteCount The number of elements to remove
1

This is what worked for me.
Class based component

this.setState({myArray: []});

With React Hook,

const [myArray, setMayArray] = React.useState([]);
setMayArray([]);

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.