How to Change Button Text on Click in ReactJS ?
Last Updated :
28 Apr, 2025
In this article, we will learn about diverse methods for altering button text dynamically upon clicking on the button. React, a widely embraced JavaScript library for crafting user interfaces provides several approaches to seamlessly achieve this functionality. The key lies in manipulating the component's state and adjusting its rendering logic to reflect the updated button text based on user interactions.
We will discuss the following two approaches to change button text on click in React:
Prerequisites
Steps to Create React Application:
Step 1: Create a react application by using this command
npx create-react-app <<Project-Name>>
Step 2: After creating your project folder, use the following command to navigate to it
cd <<Project-Name>>
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Approach 1: Using setTimeout() Method
In this method, the button text swiftly changes to "Loading..." on click using `setTimeout()` and `setButtonText()`. After a deliberate 2-second delay, the original text is restored, creating a brief loading state for enhanced user experience.
Example: Below is the code example of the above explained approach
JavaScript
import React, { useState } from 'react';
function App() {
const [buttonText, setButtonText] = useState('Submit');
const handleClick = () => {
setButtonText('Loading...');
setTimeout(() => {
setButtonText('Submit');
}, 2000); // Reverts back to 'Submit' after 2 seconds
};
return (
<div style={styles.container}>
<h1 style={styles.heading}>
Geeksforgeeks
</h1>
<button onClick={handleClick}
style={styles.btn}>
{buttonText}
</button>
</div>
);
}
export default App;
const styles = {
container: {
textAlign: 'center',
margin: 'auto',
padding: '20px',
width: 400,
},
heading: {
fontSize: '34px',
marginBottom: '10px',
color: "green",
borderBottom: "3px solid crimson",
paddingBottom: 20,
borderRadius: "8px",
},
btn: {
backgroundColor: 'green',
fontSize: 20,
color: 'white',
cursor: 'pointer',
margin: 10,
padding: 15,
borderRadius: "8px",
border: "none",
boxShadow: "0px 0px 10px 0px grey",
},
};
Output:

Approach 2: Using Ternary Operator
Streamline a button's text change using a ternary operator by modifying the "buttonText" state in the handleClick function. The operator evaluates the current value and switches between "Submit" and "Loading...", prompting React to re-render the updated text on each click.
Example: Below is the code example of the above explained approach
JavaScript
import React, { useState } from 'react';
function App() {
const [buttonText, setButtonText] = useState('Submit');
const handleClick = () => {
setButtonText(buttonText === 'Submit' ? 'Loading...' : 'Submit');
};
return (
<div style={styles.container}>
<h1 style={styles.heading}>Geeksforgeeks</h1>
<button onClick={handleClick}
style={styles.btn}>
{buttonText}
</button>
</div>
);
}
export default App;
const styles = {
container: {
textAlign: 'center',
margin: 'auto',
padding: '20px',
width: 400,
},
heading: {
fontSize: '34px',
marginBottom: '10px',
color: "green",
borderBottom: "3px solid crimson",
paddingBottom: 20,
borderRadius: "8px",
},
btn: {
backgroundColor: 'green',
fontSize: 20,
color: 'white',
cursor: 'pointer',
margin: 10,
padding: 15,
borderRadius: "8px",
border: "none",
boxShadow: "0px 0px 10px 0px grey",
},
};
Output:
Output
Similar Reads
How to change the state of react component on click? To change the state of the React component is useful when you are working on a single page application, it simply replaces the content of the existing component for the user without reloading the webpage. We have to set initial state value inside constructor function and set click event handler of t
2 min read
How To Enable Button Based On If statement in ReactJS? React.js is one of the most popular JavaScript libraries for building user interfaces, and one common task is enabling or disabling a button based on certain conditions or user inputs. This is often achieved by using conditional logic such as if statements.In this article, we'll explore how to enabl
2 min read
How to create Shopping Cart Button in ReactJS? A shopping cart button is one of the most used component in e-commerce websites or applications which allows users to add items to their cart . In this tutorial, we will learn how to create a shopping cart button in ReactJS using Material-UI. PrerequisitesJavaScript and JSXReact JSSteps to create Re
3 min read
How to Create Button in React-Native App ? React Native provides pre-defined components like button and TouchableOpacity to create buttons in a react native app. In this article, we will see how to create buttons in react-native, their syntax, and different types of buttons available in react-native.Table of ContentApproachButton in React Na
4 min read
How to Change Colors for a Button in React-Bootstrap? In this tutorial, we will learn how to change the colors of buttons in React-Bootstrap. The Button component in React-Bootstrap is a simple button with CSS and the effects of Boostrap. We can apply different colors based on the different situations like Error, Success, etc. Steps to create the appli
2 min read