Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve a problem with a higher order function?

I don’t seem to fully understand how "closing" works. I have an input when changing which I need to call functions and pass 2 arguments to it. But I also need the event of this event.

My version below doesn’t work. Only the first part of the function will work, and the second (where I return the second function) does not work. I can not, unfortunately, understand what is my mistake and how to make the "closure" to the end.

And the second, mini, question – how exactly to type event here if I’m working in react?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const handleChangeClosure = (userId: string, role: string) => (event: ???) => {
  const { checked } = event.target;
  dispatch(addOrDeleteRoleAction(userId, role, checked, dispatch));
};

<input type="checkbox" onChange={() => handleChangeClosure(user.userId, "Controller")}/>

>Solution :

Replace

<input type="checkbox" onChange={() => handleChangeClosure(user.userId, "Controller")}/>

With

<input type="checkbox" onChange={handleChangeClosure(user.userId, "Controller")}/>

The handleChangeClosure function returns a function and the onChange event expects a function. So you don’t need ()=> which would just wrap it in another function.

Alternatively, this would also work:

<input type="checkbox" onChange={(ev) => handleChangeClosure(user.userId, "Controller")(ev)}/>

But that’s just more useless code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading