React Reference ยท Built-in DOM Components
The React button element
Reference for the React button element: handle clicks, set type to avoid accidental form submits, disable while pending and use formAction in React 19.
<button> is the element for things the user can press. In React you attach behavior with onClick, and you control its availability with disabled. Inside a <form>, a button is also a submit control, which is where most button bugs come from: the default type of a button is submit, so a button you meant as a plain helper submits the surrounding form.
React 19 adds formAction: a function that runs instead of the form's action when this specific button is used to submit. That lets one form have several outcomes, such as Save and Publish, without extra state.
<button type="button" onClick={() => setCount(count + 1)}> Add one </button>
Try it
Syntax
<button type="button | submit | reset" onClick={(event) => {}} disabled={boolean} formAction={string | ((formData: FormData) => void)} > Label </button>
| Name | Description |
|---|---|
type |
submit (default), button or reset. Always set it explicitly. |
onClick |
Called on click, including keyboard activation with Enter or Space. |
disabled |
Disables the button: no clicks, not focusable, not submitted. |
formAction |
A URL string or a function. Overrides the parent form's action for this button. |
formMethod, formEncType, formTarget, formNoValidate |
Per-button overrides of the form's attributes. |
name, value |
Sent with the form when this button was the submitter. Useful to tell buttons apart. |
form |
The id of a form the button belongs to when it is not inside it. |
popoverTarget, popoverTargetAction |
Control a popover element. |
Returns: an HTMLButtonElement.
Edge cases: React does not fire onClick on a disabled button.
Usage notes
- Always set
type. Usetype="button"for anything that is not meant to submit. Usetype="submit"for the one that is. A forgotten type is the most common cause of a "page reloads when I click" bug. - Pass a function, do not call it.
onClick={handle}oronClick={() => handle(id)}, neveronClick={handle(id)}, which runs during render. - Use a button, not a
<div>. Buttons are focusable, respond to Enter and Space, and are announced as buttons. If a click navigates, use<a>instead. - Disabled and accessibility. Disabled buttons are skipped by keyboard focus. If the user needs to learn why it is disabled, consider
aria-disabled="true"plus a message, and ignore the click in the handler. - Double submits. Disable the button, or ignore clicks, while an async action runs. Inside a form action,
useFormStatus().pendinggives you this state. - Which button submitted? In
onSubmit,event.nativeEvent.submitteris the button. In an action, give buttons differentformActionfunctions or aname/valuepair and read it fromFormData. - Icon-only buttons need a name. Add
aria-label="Close"when the content is just an icon. - Event handling. React delegates events at the root;
event.stopPropagation()stops React's own bubbling, but order relative to native listeners added withaddEventListenercan surprise you.
Related
<form>,<a>,<input>- Lessons: Handling events, Form actions