Thanh Pham
Thanh Pham

Reputation: 61

Create a new object having the pair key and value from TextInput

I'd like to create a new object. The key and value of the new object are the inputs from TextInput. Below please see my code. Obviously when I hit submit, the new object will always take the string 'key' as its key and not the value that I input. How do I fix it? Thank you.

state = {
 list: [{key1:1},{key2:2}],
 key:'',
 value:0,
}
_onSubmit = () =>{
 let {list,key,value} = this.state;
 let myList = [...list];
 let newProp = {};
 newProp.key = value;
 myList.push(newProp);
 this.setState({list: myList});
}

Upvotes: 0

Views: 48

Answers (1)

basudz
basudz

Reputation: 1037

I think what you are looking for is newProp[key] = value;

Upvotes: 1

Related Questions