I am trying to change the data in object using setState 'react' method. But instead of writing function for each key in the state object, I tried create one object and return key from that object. e.g. I am using class method for creating components
state = {
value1: 'value place',
value2: 'value 2 placed',
}
handleChange = (e) => {
this.setState({
// here i am putting the setstate keyname from the element. This returns string for which
// this won't work.
this.props.keyname = e.target.value,
})
}
render(){
return (<div>
<h1>{this.state.value1}</h1>
<input type="text" value={this.state.value1} onChange={this.handleChange} keyname={Object.keys(this.state)[0]}/>
</div>)
}
As seen above I want to pass on the keyname of that state and get it in the setState using props. But because the returned value is a string, this solution won't work. Is there anyways of getting this string work. Thanks