Skip to content

Instantly share code, notes, and snippets.

@kingluddite
Created February 1, 2024 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingluddite/50d1024ce84005fb07e1eb18aeee79e1 to your computer and use it in GitHub Desktop.
Save kingluddite/50d1024ce84005fb07e1eb18aeee79e1 to your computer and use it in GitHub Desktop.
In react what the difference between passing a function inline to an event handler like onClick or onChange and referencing a function?

In react what the difference between passing a function inline to an event handler like onClick or onChange and referencing a function?

In React functional components, the same principles apply, but the syntax is a bit different. Let's go through both scenarios using functional components.

  1. Referencing a Function:

    import React from 'react';
    
    const MyComponent = () => {
      const handleClick = () => {
        console.log('Button clicked!');
      };
    
      return (
        <button onClick={handleClick}>Click me</button>
      );
    };
    
    export default MyComponent;

    In this example, handleClick is a function defined within the functional component. When the button is clicked, handleClick will be invoked.

    Advantages:

    • Code separation: The logic for handling the event is defined in a separate function, keeping the component's logic organized.
    • Easier testing: Standalone functions are generally easier to test.
  2. Inline Function:

    import React from 'react';
    
    const MyComponent = () => {
      return (
        <button onClick={() => console.log('Button clicked!')}>Click me</button>
      );
    };
    
    export default MyComponent;

    In this example, an inline arrow function is used directly within the onClick attribute. The arrow function contains the logic to be executed when the button is clicked.

    Advantages:

    • Conciseness: For simple logic, using an inline function can be more concise than defining a separate function.
    • Access to local scope: Inline functions have access to the component's local scope, including state and props.

Considerations:

  • Performance: Using inline functions in functional components can also create a new function instance on every render, potentially leading to unnecessary re-renders. If performance is a concern, you might consider using the useCallback hook to memoize the function reference.
import React, { useCallback } from 'react';

const MyComponent = () => {
  const handleClick = useCallback(() => {
    console.log('Button clicked!');
  }, []);

  return (
    <button onClick={handleClick}>Click me</button>
  );
};

export default MyComponent;

Choose the approach that aligns with your code organization, readability, and performance considerations in the context of functional components.

What happens if I use the following instead?

<button onClick={ console.log('Button clicked!')}>Click me</button>

If you use the above syntax it would not work as expected. The console.log('Button clicked!') expression would be immediately executed when the component renders, not when the button is clicked. This is because you are invoking console.log directly instead of passing a function reference.

To make it work as an event handler, you should pass a function reference instead. You can use an inline arrow function for this purpose:

<button onClick={() => console.log('Button clicked!')}>Click me</button>

This way, the console.log('Button clicked!') will be executed when the button is clicked, not during the initial rendering of the component. Always ensure that you pass a function reference or use a function declaration to handle events in React components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment