How do I edit multiple input controlled components in React?

后端 未结 9 510
[愿得一人]
[愿得一人] 2020-12-04 09:01

I have a component that stores a contact object as state - {firstName: \"John\", lastName: \"Doe\", phone: \"1234567890} I want to create a form to edit this object but if I

9条回答
  •  攒了一身酷
    2020-12-04 09:35

    elements often have a property called name. We can access this name property from the event object that we receive from an event handler:

    Write a generalized change handler

    constructor () {
        super();
        this.state = {
          name: '',
          age: ''
        };
        this.handleChange = this.handleChange.bind(this);
      }
      handleChange (evt) {      
        this.setState({ [evt.target.name]: evt.target.value });
      }
    
    render () {
        return (
          
    ); }

    source

提交回复
热议问题