最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - event.target null in React event handler - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jan 9, 2022 at 14:36 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 3, 2016 at 18:52 Jack ReidJack Reid 872 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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

update(event) {
       ^^^^^

Example

发布评论

评论列表(0)

  1. 暂无评论