React Js Change Button Color on Click

Learn how to change the color of a button or any element on click in React using the useState hook and the onClick event handler. Follow simple examples and explanations to create dynamic and interactive UI components with React. Whether you want to change the background color, the text color, or the style of a button on click, this tutorial will show you how to do it with React.




Thanks for your feedback!
Your contributions will help us to improve service.
How to Change Button Color in React Js
React Js Change Color Button on Click: To change a button's color in React.js onClick, you can create a state variable to track the button's color and use the useState
hook. Initially, set the color to its default value. When the button is clicked, use the onClick
event handler to update the state variable with a new color. Then, apply this color to the button's style using inline CSS or a CSS class with conditional rendering. This way, when the button is clicked, React re-renders the component with the updated color, giving the appearance of a color change.
React Js Change Color Button on Click Example
xxxxxxxxxx
<script type="text/babel">
const { useState } = React;
function App() {
const [btnClass, setBtnClass] = useState("blue-color");
function toggleColor() {
setBtnClass(
btnClass === "blue-color" ? "orange-color" : "blue-color"
);
}
return (
<div className="container">
<h2>React Js Change Color button on Click</h2>
<button onClick={toggleColor} className={`${btnClass} my-btn`}>
Toggle Color
</button>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of Change Button Color on click React
It will appear as shown below after clicking the button:
How to Change Background Color when Click on Button in React Js
This Reactjs code creates a button that changes its color when clicked. The button's color is managed using the state variable "isClick," initialized as true (red). When the button is clicked, the "toggleColor" function is called, which toggles the state value between true and false. The button's inline style uses a ternary operator to set the background color based on the "isClick" state (green when true, cyan when false).
React Change Button Color onclick using lnline style
xxxxxxxxxx
<script type="text/babel" data-presets="env,react">
const { useState } = React;
function App() {
// Step 1: Create a state variable to store the current color value
const [isClick, setIsClick] = useState(true); // Set the initial color to red
// Step 3: Implement a function to toggle the color when the button is clicked
const toggleColor = () => {
setIsClick(!isClick); // Toggle the state between true and false
};
return (
<div className="container">
<h3>React Js Change Button Color onclick using inline style</h3>
{/* Step 2: Use a ternary operator in the button's inline style to conditionally set the color */}
<button
style={{
backgroundColor: isClick ? "#00897B" : "#26C6DA",
}}
onClick={toggleColor}
>
Change Button Color onclick
</button>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js Change Button Color onclick using lnline style
How does it enable the change of a button's background color upon clicking?
This React.js code snippet creates a button that changes its background color on click using CSS toggling. It uses the useState hook to manage a state variable isButtonActive, which tracks whether the button is active. The button's class name is conditionally set based on the isButtonActive state, adding the class 'active-button' when the button is active. CSS styles associated with 'active-button' can be defined to change the button's background color. When the button is clicked, setIsButtonActive toggles the state, causing the button's class to change and alter its background color, providing a visual indication of its state.
React Change Button Color onclick Hooks
xxxxxxxxxx
<script type="text/babel">
const { useState } = React;
function App() {
const [isButtonActive, setIsButtonActive] = useState(false);
return (
<div className='container'>
<h3>React Js Change Button Color onclick| Background Color</h3>
<button
className={`my-button ${isButtonActive ? 'active-button' : ''}`}
onClick={() => setIsButtonActive(!isButtonActive)}
>
Toggle Button Background Color
</button>
</div>
);
} ReactDOM.render(<App />, document.getElementById('app'));
</script>
Output of React Js Change Button Color onclick using Toggle CSS
Before Click
After Click