react onclick pass event and parameter code example

Example 1: react onclick parameter

import React from 'react';

const ExampleComponent = () => {
  
  function sayHello(name) {
    alert(`hello, ${name}`);
  }
  
  return (
    <button onClick={() => sayHello('James')}>Greet</button>
  );
}

export default ExampleComponent;

Example 2: how to pass a value to a react funtion without immediately firing it

<button onClick={(e) => {
     this.clickMe(e, someParameter)
}}>Click Me!</button>

Example 3: how to call a function in react with arguments onclick

<button onClick={() => sayHello('James')}>Greet</button>

Example 4: get syntethicbaseevent and parameter in react

Try this:

<button onClick={(e) => clickMe(e, someParameter)}>Click Me!</button>

And in your function:

function clickMe(event, someParameter){
     //do with event
}