Data binding in React

前端 未结 12 774
心在旅途
心在旅途 2021-01-30 19:49

What I want to do is when I type some text in an input field, it should appear in another place realtime.

Below is my input;



        
12条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 20:52

    I think @Richard Garside is correct.

    I suggest some changes to clear even more the code.

    Change this

    onChange={(e) => this.update("field2", e)}
    

    To this

    onChange={this.handleOnChange}
    

    And also, change this

    this.setState({ [name]: e.target.value });
    

    To this

    this.setState({ [e.target.name]: e.target.value})
    

    Besides, you have to add the "name" attribute to the field with a value that relates with the key on the state object.

提交回复
热议问题