Forms

Interactive Props

  • value,checked,selected可以根据用户的输入而改变
  • onChange用来对以上属性的变化进行响应

Controlled Components & Uncontrolled Components

受控组件:有value属性,并且根据onChange来变化;A Controlled component does not maintain its own internal state; the component renders purely based on props.

举个🌰:<input>为受控组件

getInitialState: function() {
    return {value: 'Hello!'};
  },
  handleChange: function(event) {
    this.setState({value: event.target.value});
  },
  render: function() {
    return (
      <input
        type="text"
        value={this.state.value}
        onChange={this.handleChange}
      />
    );
  }

不受控组件:无value,用户输入什么就显示什么;An uncontrolled component maintains its own internal state.

举个🌰:

render: function() {
    return <input type="text" />;
  }

results matching ""

    No results matching ""