Jack Reid
Jack Reid

Reputation: 87

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?

Upvotes: 0

Views: 9408

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

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

update(event) {
       ^^^^^

Example

Upvotes: 5

Related Questions