简体   繁体   中英

event.target null in React event handler

Given the following code:

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;

Every time I log the event object in update(), it returns an object with target: null , and this.state.value updates from "" to undefined . This code diverges very little from the example on the Forms docs , why can't I seem to get an event target?

Add event ( you can name it as you want not only event ) argument to update method

update(event) {
       ^^^^^

Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM