1

In React, I can call the function in onClick in two different ways.

First Method: Arrow Function

const Modal = ({isOpen, toggle, children}) => {
    <div onClick={() => toggle()}>
      <div>
        {children}
      </div>
    </div>
}

Second Method: Without brackets

   const Modal = ({isOpen, toggle, children}) => 
   {
     return(
     <div onClick={toggle}>
       
     </div>
     )
   }

What is the difference between them? () => toggle() <-> toggle

4
  • 3
    The second method will pass all the arguments from an onclick event to toggle, even if it's not expecting and doesn't use them. The first method passes no arguments to toggle. Commented Jan 15, 2022 at 10:05
  • There has to be a good dupe target for this... Commented Jan 15, 2022 at 10:11
  • 1
    Picking up from @JackBashford: The first method creates a new function that, when called, will call toggle with no arguments. Commented Jan 15, 2022 at 10:12
  • 1
    I forgot to mention that part, thanks @T.J.Crowder. Commented Jan 15, 2022 at 12:47

3 Answers 3

3

The first way, onClick={() => toggle()}, creates a new function and assigns it to the onClick property. That function, when called, will call toggle with no arguments. A new function is created every time that component is rendered.

The second way, onClick={toggle}, does not create a new function, it directly assigns toggle to the onClick property. That means that when it's called, it receives any arguments that are passed (even if it doesn't expect any).

Each can be appropriate, depending on what you want to do. If toggle expects the arguments the click event will pass it, you're better off with the second way since you aren't creating a new function every time. If it doesn't, in general it's best not to set it up to receive an argument it doesn't expect.

The fact that the first way creates a new function on every render probably doesn't matter when you do this with a DOM element like a div, but suppose you're passing a function to a complex component that takes time to render and that optimizes render (avoiding re-rendering if its props don't change, via React.memo, PureComponent, shouldComponentUpdate, etc.):

return <SomeComplexComponent onSomething={() => toggle()} />

In that case, you might be best off "memoizing" the function (in this case, via useCallback or useMemo, usually) so you don't pass a new one to the component every time, so the complex component doesn't think it needs to re-render every time.

const onSomething = useCallback(() => toggle(), [toggle]);
// ...
return <SomeComplexComponent onSomething={onSomething} />

Although that still creates a new function on every render (so it can be passed into useCallback), useCallback will return the previous version of the function if toggle hasn't changed, which allows SomeComplexComponent to avoid re-rendering.

But there's no need for that when passing this to an HTML element or a simple component.

Sign up to request clarification or add additional context in comments.

2 Comments

Also isn't this different in both methods ?
@EEAH - In the general case, yes, good point. It probably isn't in this specific case, since React doesn't set this when calling event handlers or component functions (so this is usually undefined). But yes, the first way this will be whatever it is where the arrow function is created, and the second way this will be whatever it's set to by what calls the function (assuming toggle isn't an arrow function or a bound function).
1

1st kind of code

onClick={toggle}

In this kind of onClick method you can call only one function inside onClick.

2nd kind of code

Suppose you need to call multiple functions inside the onClick. Then you have to use

onClick={() => toggle()}

Exmaple:-

OnClick = {()=>{
 toggle1();
 toggle2();
}}

And also you can pass parameters inside the function. using 2nd type like below. Example: -

OnClick = {()=>{
     person(name);
    }}

1 Comment

This is technically correct, but an incomplete answer.
0

In () => toggle() you are adding an anonymous function and then calling toggle function. If you want to pass in some arguments to toggle function, you can use this method. eg () => toggle(id, name..etc)

The other method simply add event handler and that function will receive event as argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.