Having the following code,
this.state = {
valores: ['A', 'B', 'C']
}
How could I update an element of this array? Of the kind:
this.setState({
valores[1]: 'a'
})
Having the following code,
this.state = {
valores: ['A', 'B', 'C']
}
How could I update an element of this array? Of the kind:
this.setState({
valores[1]: 'a'
})
I recommend not directly mutating the state object, instead assigning it to a variable and reassigning it.
Here you can read some reasons why you should not mutate directly https://daveceddia.com/why-not-modify-react-state-directly/
You could also use https://github.com/kolodny/immutability-helper
It is not at all recommended to use auxiliary variables since in components that handle a more complex state it becomes unmanageable to mutate the state in this way, for this React provides Helpers that help you to mutate the state when its structure becomes more complex
https://facebook.github.io/react/docs/update.html
So, for example, assuming that your component handles more variables in the state apart from 'values', using React's Inmutability Helpers, the implementation would be as follows (ES6)
In this way you only update the 'values' array and leave the rest of the state structure intact without the need to make unnecessary copies or use auxiliary variables.