How to Prevent Default Behavior in an Event Callback in React JS ?
Last Updated :
12 Apr, 2025
React JS provides events to create interactive and responsive user interfaces. One characteristic of event handling is to prevent the default behavior of events in certain cases. This article covers the process of preventing default behavior in event callbacks within React JS.
Prerequisites:
Approach
To prevent default behaviour in event callback in react we will be using the preventDefault method. It is used to prevent the browser from executing any default action.
Steps to Create React Application
Step 1: Initialize the react app using this command in the terminal.
npx create-react-app myapp
Step 2: After creating your project folder, i.e. myapp, move to it using the following command:
cd myapp
Project Structure:
Initial folder Structure The updated dependencies in package.json file will look like:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Step 3: Create a basic input form to take the name input without using preventDefault funtion.
JavaScript
// Filename - App.js
import React, { useState } from "react";
function App() {
const [name, setName] = useState("");
const handleSubmit = () => {
alert("There will be a browser reload once the alert box is closed.");
};
return (
<form onSubmit={handleSubmit}>
<input
type="text"
value={name}
placeholder="Name"
onChange={(e) => setName(e.target.value)}
/>
<button type="submit">Click</button>
</form>
);
}
export default App;
We have a form element with a submit event, wrapping a button. When the button is clicked, the handleSubmit function is triggered.
Step to run the application: Use the following command to run the app and open http://localhost:3000
npm start
Output:
Preventing default behaviour
We can prevent this default behavior by making a small modification to the definition of the handleSubmit function. We call a preventDefault on the event when submitting the form, and this will cancel the default event behavior (browser refresh) while allowing us to execute any code we write inside handleSubmit.
Example: This example uses preventDefault method to prevent browsers default actions on the input field and input form.
JavaScript
// Filename - App.js
import React, { useState } from "react";
function App() {
const [name, setName] = useState("");
const handleSubmit = (event) => {
event.preventDefault();
alert("The browser will not reload when the alert box is closed.");
};
return (
<form onSubmit={handleSubmit}>
<input
type="text"
value={name}
placeholder="Name"
onChange={(e) => setName(e.target.value)}
/>
<button type="submit">Click</button>
</form>
);
}
export default App;
Output: This is the new behavior
Conclusion
The preventDefault method is used to prevent the default browser actions hence we can use it to prevern the default action in event callbacks.
Similar Reads
How to prevent the default action of an event in JavaScript ? The term "default action" usually refers to the default behavior or action that occurs when an event is triggered. Sometimes, it becomes necessary to prevent a default action of an event. Let's create HTML structure with event and function that needs to prevent the default actions. HTML <!DOCTYPE
3 min read
How to bind an event handler in JSX callback ? ReactJS is a JavaScript library focused on building user interfaces. JSX callbacks in React are used to associate event handlers with elements in JSX code. Event handlers, like those for button clicks or form submissions, are functions triggered by events. In React, event handlers are bound to eleme
4 min read
How to create an event in React ? In the development of contemporary webpages, user interactions play a pivotal role. These interactions include mouse clicks, keypresses, or even uncommon events like connecting a battery to a charger. As developers, our task is to actively "listen" to these events and subsequently ensure that our ap
2 min read
How to Conditionally Disable a Form Input in React-Bootstrap ? In this article, we will see how to conditionally disable a form input in React-Bootstrap. React-Bootstrap is a popular open-source library for building frontend components with the advantage of the pre-build component features of Bootstrap. Creating React Application And Installing ModuleStep 1: Cr
3 min read
How to avoid binding by using arrow functions in callbacks in ReactJS? In React class-based components when we use event handler callbacks, it is very important to give special attention to the 'this' keyword. In these cases the context this is undefined when the callback function actually gets invoked that's why we have to bind the context of this. Now if binding all
2 min read