I am passing an array which contains the following credentials.:
//Snippet variable contains an array which looks like this
{
details: test,
_id: "5d9d0b506ee7d03a54d8b1f6",
name: "TEST GREEN",
content: "<div style="background-color:green; height:100px; width:100px;"></div>",
}
The array is recieved by a function, the function updates the state. The state looks like this:
constructor(props) {
super(props)
this.showModal = React.createRef()
this.state = {
snippets: [],
edit: null,
show: false,
sid: '',
}
}
The function looks like this
handleModal = snippet => {
console.log(snippet) //pastes output found in the snippet array above
console.log(this.state) //{snippets: Array(3), edit: null, show: false, sid: ""}
this.setState({ edit: this.snippet })
console.log(this.state)//{snippets: Array(3), edit: null, show: false, sid: ""}
this.setState({ sid: snippet._id })
this.showModal.current.showModal()
}
I know I have two setStates. I am trying to isolate the problem.
edit: null in the state should be becoming edit: Array(1) but setState seems to be failing.
setStateyou have used value foreditasthis.snippet. Wouldn't you usesnippetinstead ofthis.snippet?setStateis async, so logging directly after the function call won't do you much good unfortunately as it will not contain the updated values.setState({edit: snippet})and notthis.snippetsetStatecall. For example:this.setState({ edit: snippet, sid: snippet._id });