ReactJS: onClick={someFunction} VS onClick={()=>someFunction}

The difference between onClick={someFunction} and onClick={() => someFunction} in React (or JavaScript in general) lies in how and when they handle the execution of the someFunction when the click event occurs :

onClick={someFunction}

D…


This content originally appeared on DEV Community and was authored by Sayyed Asad Ullah

The difference between onClick={someFunction} and onClick={() => someFunction} in React (or JavaScript in general) lies in how and when they handle the execution of the someFunction when the click event occurs :

onClick={someFunction}

  • Direct Reference: This syntax directly references the function someFunction.

  • When Clicked: The function someFunction will be called when the onClick event is triggered (e.g., when the element is clicked).

  • No Extra Function Call: No additional function is created; React simply uses the function reference you provided.

  • Example: If someFunction is defined as function someFunction() { console.log('clicked'); }, then onClick={someFunction} will log 'clicked' when the element is clicked.

onClick={() => someFunction()}

  • Arrow Function: This syntax uses an arrow function to call someFunction.

  • Creates a New Function: Each time the component renders, a new function is created. The arrow function wraps the call to someFunction.

  • Immediate Invocation: Within the arrow function, someFunction is called immediately when the onClick event is triggered.

  • Use Case: Useful when you need to pass arguments to the function or need to do additional work before calling someFunction.

  • Example: If you want to pass an argument to someFunction, you can use onClick={() => someFunction('argument')}. This will call someFunction with 'argument' when the element is clicked.

When to Use Each

  • Direct Reference ({onClick={someFunction}):
  1. Use this when you want to avoid creating an extra function on each render, which can be more efficient.

  2. Preferable for simple event handlers where no additional arguments or operations are needed.

  • Arrow Function (onClick={() => someFunction()}):
  1. Use this when you need to pass arguments to the function or perform additional operations before calling the function.

  2. Useful for inline operations or when dealing with closures.

Code Example

You can understand via a code example.

//Direct Reference

function handleClick () {
console.log('Clicked Button');
}

// Arrow Function 

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

<button onClick={() => handleClick('Button clicked')}>Click Me</button>


Understanding these differences helps in optimizing performance and achieving the desired behavior in React components.


This content originally appeared on DEV Community and was authored by Sayyed Asad Ullah


Print Share Comment Cite Upload Translate Updates
APA

Sayyed Asad Ullah | Sciencx (2024-06-24T05:32:32+00:00) ReactJS: onClick={someFunction} VS onClick={()=>someFunction}. Retrieved from https://www.scien.cx/2024/06/24/reactjs-onclicksomefunction-vs-onclicksomefunction/

MLA
" » ReactJS: onClick={someFunction} VS onClick={()=>someFunction}." Sayyed Asad Ullah | Sciencx - Monday June 24, 2024, https://www.scien.cx/2024/06/24/reactjs-onclicksomefunction-vs-onclicksomefunction/
HARVARD
Sayyed Asad Ullah | Sciencx Monday June 24, 2024 » ReactJS: onClick={someFunction} VS onClick={()=>someFunction}., viewed ,<https://www.scien.cx/2024/06/24/reactjs-onclicksomefunction-vs-onclicksomefunction/>
VANCOUVER
Sayyed Asad Ullah | Sciencx - » ReactJS: onClick={someFunction} VS onClick={()=>someFunction}. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/24/reactjs-onclicksomefunction-vs-onclicksomefunction/
CHICAGO
" » ReactJS: onClick={someFunction} VS onClick={()=>someFunction}." Sayyed Asad Ullah | Sciencx - Accessed . https://www.scien.cx/2024/06/24/reactjs-onclicksomefunction-vs-onclicksomefunction/
IEEE
" » ReactJS: onClick={someFunction} VS onClick={()=>someFunction}." Sayyed Asad Ullah | Sciencx [Online]. Available: https://www.scien.cx/2024/06/24/reactjs-onclicksomefunction-vs-onclicksomefunction/. [Accessed: ]
rf:citation
» ReactJS: onClick={someFunction} VS onClick={()=>someFunction} | Sayyed Asad Ullah | Sciencx | https://www.scien.cx/2024/06/24/reactjs-onclicksomefunction-vs-onclicksomefunction/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.