React事件处理程序中的event.target null

Jac*_*eid 1 javascript event-handling javascript-events reactjs

给出以下代码:

var
  React = require("react")
;

class ControlText extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      value: ""
    };
  }

  update() {
    console.log(event);
    this.setState({value: event.target.value});
  }

  render() {
    console.log(this.state);
    var value = this.state.value;
    return <input type="text" value={value} onChange={this.update.bind(this)} />
  }
}

module.exports = ControlText;
Run Code Online (Sandbox Code Playgroud)

每次我登录事件对象的update(),它返回一个对象target: null,并this.state.value更新从""undefined.这段代码与Forms文档上的示例差异很小,为什么我似乎无法获得事件目标?

Ale*_* T. 5

添加event(您可以根据需要命名event)update方法的参数

update(event) {
       ^^^^^
Run Code Online (Sandbox Code Playgroud)

Example