Home > Blockchain >  How to set state when a button is clicked
How to set state when a button is clicked

Time:02-22

I want to set user.Id in setDelid when the button is clicked.

<div className="users">
    {users.map((user) => {
      return (
        <div className="users" key={user.Id} >
          <p>
            Name: {user.Title}, EmpID: {user.EmpID}, ID: {user.Id} 
          </p>
          <button onClick={deleteData} onChange={(user) => setDelid(user.Id)} className="button button4"></button>
        </div>
      );
    })}
  </div>

CodePudding user response:

You can use the onClick function to run the deleteData function and set state:

<button onClick={() => handleClick(user.id)} />

const handleClick = (userId: string) => {
  setDelid(userId)
  deleteData()
}
  • Related