Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use inline function on button onClick event - Javascript/React.js

I can see different style of assigning event handler to the button onClick event. Can anyone suggest when to use inline function on button onClick event handler?

onClick={props.handleDeleteOption(props.optionText)}; // Call the handler directly

onClick={(e) => {
          props.handleDeleteOption(props.optionText);
        }} // Call the handler through inline-function
like image 565
Prem Avatar asked May 15 '18 12:05

Prem


2 Answers

Performance

Whether or not you use inline event handler functions it has an impact on the performance of the app.

Inline event handlers are anonymous functions. They are created every time the component renders. That is, every time you call setState or when the component receives new props.

Whenever a component is about to be rendered (when the state has been updated or receives new props,) react performs a shallow comparison between the previous DOM and the new DOM for that component. If they are found to have different props from the props before the state update, then the component will re-render that particular component and all its child components. If not, it assumes the component is the same as the old DOM and so it doesn't render it.

Now inline functions are objects (functions are objects in javascript.) And when react compares functions, it does a strict comparison. The inline function might not have changed in its value, but it is an entirely different function (different reference in memory) and so React detects that there has been a change. And when there is a change, React re-renders the component and all its children.

Again, let me just state that performance decisions are usually largely tradeoffs. This explanation doesn't mean that you should remove all inline event handlers and define them at the class level. This can slow down the first render for your component because of the binding that has to be done in the constructor. There is also this thing called premature optimization which can lead to poor quality of code or it just might not be worth it.

like image 119
Awa Melvine Avatar answered Oct 06 '22 01:10

Awa Melvine


onClick={props.handleDeleteOption(props.optionText)}; this will cause props.handleDeleteOption(props.optionText)} to be called without clicking the button.

In javascript, let there be a function called foo, foo() will call the function whereas foo itself will be the reference to that function.

So when you do as in the second case, the function is passed to the onClick handler and will be triggered only onClick. The first line of code will not work as expected since it is being called there itself. If you did not have to pass any data to the function, you could also have had written onClick={props.handleDeleteOption}(notice there are no brackets), and that would work as expected.

But since there is data, the only way you can write it is by onClick={(e) => {props.handleDeleteOption(props.optionText)}}

like image 38
illiteratewriter Avatar answered Oct 06 '22 01:10

illiteratewriter