preventDefault() cancels an event’s browser default action when the event is cancelable. React event objects provide this method through an API based on HTML events. section 8.4.2 section 8.2.3
For forms, handle the form’s onSubmit event and call evt.preventDefault(). This prevents the browser from submitting to the target URL and reloading the page. section 8.4.2
React form submission
const onSubmit = (evt) => { evt.preventDefault(); if (password === "platypus") setAdmin(true); }; <form onSubmit={onSubmit}> <input type="password" onChange={(evt) => setPassword(evt.target.value)} /> <button>Login</button> </form> Putting the handler on the form handles both clicking Login and pressing Enter in the input. section 8.4.2
The method also cancels other browser actions, such as following a link, scrolling, inserting a typed character, or starting a drag. Check evt.cancellable first when the event type matters; noncancellable events ignore the call. section 8.4.3
