Reactjs -How can recognise which button was clicked?

I have three buttons


I want to have one handler function that takes the action based on clicked button

const clickHandler=()=>{
      if(A was clicked)
               do this 
      if (B was clicked)
               do this
      if (C was clicked)
                do this

how do I recognize which button was clicked?


export default function App() {
  const handleButtonCLick = (e) => {
  return (
    <div className="App">
      <button id={1} onClick={(e) => handleButtonCLick(e)}>
      <button id={2} onClick={(e) => handleButtonCLick(e)}>
      <button id={3} onClick={(e) => handleButtonCLick(e)}>
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
1 Like

Thanks for your solution.
quick question,
Can I have just a numeric id or it can be a string? do I have to wrap them up in braces or just within the quote
can I have this ?

<button id="mealType">A</button>

go to codesandbox choose react, you can play with it copy and paste my sample from above

Never mind, it’s working thanks for your answer.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.