Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
How does nested function in an event handler receive the event object?

How does the nested function inside handleClick2 get the e object, when i am only passing the input object ? Like how is the object e available for the scope of the nested function ? Does this have anything to do with Lexical Environment ?


  handleClick2 = (input) => (e) => {
    this.setState({
      [input]: e.target.value
    });
  };

render() {
        return (
              <button onClick={this.handleClick2('item')}>
                Confirm
              </button>
 
        );}

And how do i generate valid parameters for the handleClick2 function as a return value from another function ? For example:


  handleClick2 = (input) => (e) => {
    this.setState({
      [input]: e.target.value
    });
  };

handleClick1 = input => e => {
    // Mutate input and e 
    return handleClick2 with mutated input and e as parameters.

}

render() {
        return (
              <button onClick={this.handleClick1('item')}>
                Confirm
              </button>
 
        );}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Funny, you get it working, you are wondering why right ?

Ok, let's do it slowly.

  handleClick = input => {
    return e => {
      this.setState(...}
    }
  }

The first invoke handleClick(input) is only returning a function. This function accepts e as input argument. And based on button interface, onClick is an event handler which is e => {}. Therefore it works.

about 4 years ago · Juan Pablo Isaza Report

0

It's a little unclear what your code is meant to be doing since you've left a lot out.

Ideally you should be updating state when the input changes, and then doing something with that state when the button is clicked.

const { Component } = React;

class Example extends Component {

  constructor() {
    super();
    this.state = { input: '' };
  }

  handleInput = (e) => {
    this.setState({ input: e.target.value });
  }

  handleClick = () => {
    console.log(this.state.input);
  }

  render() {
    return (
      <div>
        <input onChange={this.handleInput} value={this.state.input} />
        <button onClick={this.handleClick}>Click me</button>
      </div>
    );
  }

};

// Render it
ReactDOM.render(
  <Example />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

Or the shorter functional component way:

const { useState } = React;

function Example() {

  const [ input, setInput ] = useState('');

  function handleInput(e) {
    setInput(e.target.value);
  }

  function handleClick() {
    console.log(input);
  }

  return (
    <div>
      <input onChange={handleInput} value={input} />
      <button onClick={handleClick}>Click me</button>
    </div>
  );
};

// Render it
ReactDOM.render(
  <Example />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Report

0

When the <button> gets clicked it calls whatever function that you have passed in the onClick argument with the created Event as the argument. In this case the function is this lambda:

(e) => {
  this.setState({
    input]: e.target.value
  });
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!