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

Given the following code:varReact = require("react");class ControlText extends React.Compone

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742362725a4429700.html

相关推荐

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

    Given the following code:varReact = require("react");class ControlText extends React.Compone

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信