0

Quick question, how do I change an object in a copy of state that is selected using e.target.name, and then set to e.target.value. Like if i want to change newState.age when e.target.name = age is sent.

stateUpdater = (e) => {
    const newState = this.state;
    const name = e.target.name;
    const value = e.target.value;
    // How do I change newState.(name passed from target) to become the passed value;
    this.setState({newState});
  }

1 Answer 1

1

to update state

stateUpdater = (e) => {
    const { name, value } = e.target
    this.setState({ [name]: value })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.