Saad El Qorchi
Saad El Qorchi

Reputation: 51

I would like to concatenate the name of a state in react native

I'm trying to concatenate the name of a state in set state method like this :

onCallMethod(name){     //name is a string
this.setState({myState + name : true});
}

But I found out that this is not working, I'm just beginning in React Native, how can I do that ??

Upvotes: 0

Views: 151

Answers (1)

Siraj Alam
Siraj Alam

Reputation: 10025

Seems like you want to store multiple names in your state variable. But you're creating the names dynamically from the name string.

This is simply not possible. Try to manage an array/object of names in the state and update that array/object on each call.

state = {
    names: {}
}

onCallMethod(name){
    this.setState({names: {...this.state.names, [name]: true}});
}

Upvotes: 1

Related Questions