How to get value of textbox in React?

前端 未结 2 1053
梦毁少年i
梦毁少年i 2021-01-03 19:02

I just started using React.js, and I\'m just not sure whether there is a special way to get the value of a textbox, returned in a component like this:

var Lo         


        
2条回答
  •  礼貌的吻别
    2021-01-03 19:34

    As described in documentation You need to use controlled input. To make an input - controlled you need to specify two props on it

    1. onChange - function that would set component state to an input value every time input is changed
    2. value - input value from the component state (this.state.value in example)

    Example:

      getInitialState: function() {
        return {value: 'Hello!'};
      },
      handleChange: function(event) {
        this.setState({value: event.target.value});
      },
      render: function() {
        return (
          
        );
      }
    

    More specifically about textarea - here

提交回复
热议问题