React by Patrik

Pass a Parameter to an onClick Event Handler

Another common use case for event handlers is passing a parameter to a function in React so it can be used later. For example:

import React from "react";

const App = () => {
  const sayHello = (name) => {alert(`Hello, ${name}!`);
  };

  return (
    <button onClick={() => {sayHello("John");}}>Say Hello</button>
  );
};

export default App;

Comments