Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 18065

How to update array using its index in ReactJS? I want to put the value of an object at a particular index in array using setState

$
0
0

I want to put the value of the state object 'nm', to the array 'Names[]' at a particular index 'uid'.Please check this code-

    this.state={        Names:[], nm:'', status:false, uid:''    }    upddata=(e)=>{  **      this.state.Names[this.state.uid]=this.state.nm; **`THIS LINE WORKS EXACTLY to put the updated data in array but shows warning: Do not mutate state directly. Use setState()`        this.setState({            status:false,            Names:this.state.Names,        })        this.setState({nm:''});    }

The above code is working as expected and updates the data at particular index but it is giving warning. How can do that using this.setState??I want to do this instead to avoid this warning: Do not mutate state directly. Use setState()


Viewing all articles
Browse latest Browse all 18065

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>