1
to try to make an attribute of one of the objects in an array of objects switch between true and false follows code:
Initial state:
constructor(props) {
super(props)
this.state = {
post: [{
nome: '',
conteudo: '',
comment: false
}]
}
this.handleClickComment = this.handleClickComment.bind(this)
}
Method that changes the comment attribute for each object in the array (fixed):
handleClickComment(postagem) {
postagem.comment = !postagem.comment
let i = postagem.id -1
this.setState({...this.state.post[this.state.post[i] = postagem]})
}
This method is called in an onClick event, and takes as parameter the object in question as it was mapped in another component, but whenever the event the comment attribute simply does not change. Would someone tell me what’s wrong?
Edit
People managed to solve I will update the code that worked perfect, updated the attribute value without changing the application status directly, always instantiates a new array with the new parameters. Thank you all for your help.
problem line:
let comment = this.state.comment ? false : true
, because $this.state.post has an array of information, agree? ai ta error! shouldn’t belet comment = this.state.post[indice].comment ? false : true
??? the third line also assignment does not seem correct.– novic
this.state.post.foreach(item => { item.comment = ! item.comment; })
– Lucas Brogni
@Lucasbrogni in this case the comment attribute would be changed on all objects in the array, no?
– Yago Santos
@Yagosantos yes, in that case he would always change the value, if he is do he would pass to true and vice versa.
– Lucas Brogni
@Lucasbrogni yes I know, but in the case I wanted to change only the value of the event in which occurred onClick...but I already understood the logic vlw
– Yago Santos