I have this state
this.state = {
dropdown1: false,
dropdown2: false,
dropdown3: false
}
I want to access to these dropdowns in state using this.setState but the number after 'dropdown' comes from API
onMaca = (ev) => {
this.setState({
dropdown ev: true
})
}
So I want the key to be dynamic 'dropdown1' for example.
Thanks for your answers
CodePudding user response:
you can access the object property like this object['property name']
onMaca = (ev) => {
this.state['dropdown' ev]= true;
this.setState({
...this.state
})
}
CodePudding user response:
https://codezup.com/add-dynamic-key-to-object-property-in-javascript/
You can use any of these to set key dynamically. I will try to update the answer with an example in a while for setState.