Where is the "event" argument coming from? React - Create a Controlled Input

Why don’t I have to give the “event” argument to handleChange() when I use it in the render() method? Where is it getting it from?

class ControlledInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: ''
    };
    // Change code below this line
    this.handleChange = this.handleChange.bind(this);
    // Change code above this line
  }
  // Change code below this line
  handleChange(event) {
    this.setState ({
      input: event.target.value
    });
  };
  // Change code above this line
  render() {
    return (
      <div>
        { /* Change code below this line */}
        <input value={this.state.input} onChange={this.handleChange}></input>
        { /* Change code above this line */}
        <h4>Controlled Input:</h4>
        <p>{this.state.input}</p>
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: React - Create a Controlled Input

Link to the challenge:

It is passed by default. All event handlers (i.e. click, mouseover, etc.) will pass the event object to the callback function referenced.

1 Like

Just so it is clear, this isn’t a React thing, it is a JS thing.

You also have access to the global event inside the handler but I wouldn’t suggest using that. If you need to pass more than the event to the handler you can explicitly pass the event as part of the arguments.

As a general rule, if an API uses a callback there is a high chance the callback is being passed something.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.