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: []
   })
 }
Damien Fayol
  • 958
  • 7
  • 17
Harsha Kakumanu
  • 703
  • 1
  • 7
  • 17
  • 1
    Something like this.state.myarray=[] – Antonio Feb 17 '16 at 20:09
  • Hey Sorry you are right @Antonio this.state.myarray=[] fixed my problem. – Harsha Kakumanu Feb 17 '16 at 20:18
  • Push data seems weird... I would write this.state.myArray.push(newVar) – Antonio Feb 17 '16 at 20:19
  • this.state.myArray.push(newVar) this way of pushing didnt work for me. I tried .concat too but didnt work – Harsha Kakumanu Feb 17 '16 at 20:20
  • Are you getting an error? It's not clear what's broken from your question. What does your `render` function look like? – Carl Vitullo Feb 17 '16 at 20:27
  • Sorry for the trouble given I found out the closeModal didnt get called at all on componentWillUnmount. So i Changed it back to my old code and fixed it this.setState({myArray: []}); will work. – Harsha Kakumanu Feb 17 '16 at 21:47
  • 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. – Mulan Feb 18 '16 at 08:12

5 Answers5

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
Community
  • 1
  • 1
Harsha Kakumanu
  • 703
  • 1
  • 7
  • 17
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([])
CanCoder
  • 1,073
  • 14
  • 20
1

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

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

Community
  • 1
  • 1
dskoda1
  • 109
  • 10
  • 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: [] }); – dskoda1 Feb 17 '16 at 20:14
  • 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.. – dskoda1 Feb 17 '16 at 20:17
1
this.state.array.splice();

This will Delete or truncate whole array

Tr4cEr
  • 47
  • 4
  • 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 – Safeer Jun 19 '22 at 13:07
1

This is what worked for me.
Class based component

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

With React Hook,

const [myArray, setMayArray] = React.useState([]);
setMayArray([]);
jfk
  • 4,335
  • 34
  • 27