Last updated: Apr 7, 2024
Reading timeยท4 min

To change the style of an element on click in React:
onClick prop on the element.import {useState} from 'react'; export default function App() { const [isActive, setIsActive] = useState(false); const handleClick = () => { // ๐๏ธ toggle setIsActive(current => !current); // ๐๏ธ or set to true // setIsActive(true); }; return ( <div> <button style={{ backgroundColor: isActive ? 'salmon' : '', color: isActive ? 'white' : '', }} onClick={handleClick} > Click </button> </div> ); }

We set the onClick prop on the element, so every time it is clicked, the
handleClick function is invoked.
<button style={{ backgroundColor: isActive ? 'salmon' : '', color: isActive ? 'white' : '', }} onClick={handleClick} > Click </button>
In our handleClick function, we simply
toggle the isActive state.
const handleClick = () => { // ๐๏ธ Toggle setIsActive(current => !current); // ๐๏ธ Or set to true // setIsActive(true); };
setIsActive(true) if you don't want to change the styles every time the element is clicked.You could set the state to active, e.g. setIsActive(true) if you don't want to
change the background color every time the element is clicked.
import {useState} from 'react'; export default function App() { const [isActive, setIsActive] = useState(false); const handleClick = () => { // ๐๏ธ Set to true setIsActive(true); }; return ( <div> <div style={{ backgroundColor: isActive ? 'salmon' : '', color: isActive ? 'white' : '', }} onClick={handleClick} > bobbyhadz.com </div> </div> ); }

The code sample changes the background color of the element only the first time it's clicked.
We used the
ternary operator to
conditionally set the backgroundColor style on the element.
<button style={{ backgroundColor: isActive ? 'salmon' : '', color: isActive ? 'white' : '', }} onClick={handleClick} > Click </button>
The ternary operator is very similar to an if/else statement.
In other words, if the isActive variable stores a true value, we set the
backgroundColor property to salmon, otherwise, set it to an empty string.
Alternatively, you can use the currentTarget property on the event object.
This is a three-step process:
onClick prop on the element.event.currentTarget.style object to set styles on the element.export default function App() { const handleClick = event => { event.currentTarget.style.backgroundColor = 'salmon'; event.currentTarget.style.color = 'white'; }; return ( <div> <button onClick={handleClick}>Click</button> </div> ); }

We can access the element via the currentTarget property of the event
object.
The currentTarget property on the event gives us access to the element that
the event listener is attached to.
Whereas the target property on the event gives us a reference to the element
that triggered the event (could be a descendant).
If you need to use a ref to change an element's style, check out the
following article.
The example above shows how to change the style of an element on click.
If you need to toggle the styles of an element on click, conditionally check
if the styles are present.
export default function App() { const handleClick = event => { // ๐๏ธ toggle styles on click if (event.currentTarget.style.backgroundColor) { event.currentTarget.style.backgroundColor = null; event.currentTarget.style.color = null; } else { event.currentTarget.style.backgroundColor = 'salmon'; event.currentTarget.style.color = 'white'; } }; return ( <div> <button onClick={handleClick}>Click</button> </div> ); }

If you need to set a style on the element on click, directly set the style via
the element's style object.
event.currentTarget.style.backgroundColor = 'salmon';
However, if you have to toggle the style every time the element is clicked, you have to conditionally check if the class is present and remove it if it is, otherwise, add the class.
If you need to add a class to the element, check out my other article: How to Add or Remove a Class on click in React.