Running event handler once

I have a button in react that performs a toggle action to a boolean. But I figure out that the button can be used to set and unset the state as a toggle can. But I want to change the state only one time. Is that another function instead of the toggle to perform what I want?

<button onClick={() => toggleDisabled(prevState => !prevState ) }> End your turn </button>

Answer

Assuming that the state name is toggle, make use of the disabled prop for button.

<button disabled={toggle} onClick={() => toggleDisabled(prevState => !prevState ) }> End your turn </button>

Leave a Reply

Your email address will not be published. Required fields are marked *