Reputation: 1679
Hard to explain but I want to do something like this :
constructor(props) {
super(props);
this.state = {
photoTree: "beautiful photo",
photoDoor: "photo of a door",
photoTreeSource: "/assets/tree.png",
photoDoorSource: "/assets/door.png",
}
}
doSomething(name) {
let _state = "photo" + name;
let _stateSource = _state + "Source";
console.log(this.state. + _state);
console.log(this.state. + _stateSource); //something like this
console.log(this.state._state);
}
render() {
return(
<View>
<TouchableOpacity style={style.button} onPress={this.doSomething.bind(this, "Tree")} />
<TouchableOpacity style={style.button} onPress={this.doSomething.bind(this, "Door")} />
</View>
)
}
I know that doing an object like 'photo: {Tree: "beautiful..", Door: "blablabla..."}' would be better but I want to know if we have a way of doing something like above, just by curiosity.
Upvotes: 4
Views: 14493
Reputation: 160
you can use setState instead of creating new variable initializing them.
Upvotes: -1
Reputation: 1679
If someone is interest, you can do something like that in doSomething(name) :
doSomething(name) {
let _state = "photo" + name;
console.log(this.state[_state]) // it work !
}
Upvotes: 7