Using computed value for sub key with React setState
问题 I would like to be able to use computed values for sub keys when updating state in React. I understand how to use computed values in straightforward settings like this: this.setState({ [name]: value }); But I am having trouble getting key-value computation to work for a situation like this: constructor(props) { super(props); this.state = { foo: { a: 1, b: 2 } }; } const keyToChange = 'a'; const value = 3; this.setState({ foo[keyToChange]: value }); How can I make something that works like