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!
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.
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.
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.
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
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.
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.
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});
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.
You cannot use this.state
with purpose to update state, you have to use:
this.setState(newStateObject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With