Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React JS maintain array inside state

I have been using React state to maintain some data. For ints and strings it's working well, but unfortunately arrays are not working.

In my component constructor, I have

constructor(props) {
    super(props);

    this.state = {
        terms: 5,
        myArray: []
    }

and then, I am trying to maintain it in componentDidUpdate

componentDidUpdate() {
    this.state = {
        terms: this.state.terms,
        myArray: this.state.myArray
    }

but myArray: this.state.myArray is not working. However terms: this.state.terms is working perfectly.

Can someone help!

like image 587
h_h Avatar asked Mar 31 '17 14:03

h_h


People also ask

Can I store an array in React state?

On the other hand, functional components use React hooks to track data changes of UI updates. Arrays are often used as a data source to create complex UI elements such as tables, lists, or grids. A state object can store arrays that can be updated with a response from an API or users.

How do arrays manage React state?

Arrays in React State export default App; As mentioned, the initial array state is done in the React component's constructor and afterward used within the render() method to display it. Every time the state changes, the render method will run again and display the correct state in your browser.

How to manage react state with arrays?

You have learned about different ways on how to manage React state with arrays. You can add items to an array, update an item in the array or update the whole array, and remove items from an array. Everything is possible with only JavaScript without using React for it. React is only used to set the state in the end.

How to add new item to an array in react?

While the array from the previous state is spread into a new array, so the previous arrays doesn't get mutated, the new item is added at the end of the array. Now, you can add the item at the start of the array by using const list = [state.value, ...state.list]; instead. React State: Update item in array

How to iterate over an empty array in ReactJS?

The first question can be answered with React's component constructor by simply initializing the state as as empty array: The map () method in the render () method, that is used to display the items of the array, does work, because it iterates over an empty array and thus returns no item for it. The iterator function in the map method isn't called.

How do I update the state of a component in react?

The this.setState () method on the component instance is used to update the React state. It does a shallow merge, meaning that when you update one property in the state (e.g. list), the other properties in the state stay intact.


3 Answers

Issue is you are updating the state value in a wrong way, Update the state value like this:

this.setState({
     terms: this.state.terms,
     myArray : this.state.myArray
});

As per DOC:

Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.

Update the state array like this, first create a copy of that by using slice(), then do the change and use setState to update:

let arr = this.state.myarr.slice();
arr.push('data');
this.setState({arr});
like image 175
Mayank Shukla Avatar answered Oct 10 '22 22:10

Mayank Shukla


You cannot set state directly like that since its an array you will have to append the value or else push the value.

try something like

var newArray = this.state.myArray.slice();    
newArray.push("new value");   
this.setState({myArray:newArray})

here i sliced to make it immutable.

like image 30
TRomesh Avatar answered Oct 10 '22 22:10

TRomesh


You cannot use this.state with purpose to update state, you have to use:

this.setState(newStateObject);
like image 23
cn007b Avatar answered Oct 10 '22 23:10

cn007b