Last updated: Apr 7, 2024
Reading timeยท3 min
To check if an element contains a class in React:
ref
prop on the element.classList.contains()
method on the ref object to check if the class
exists.true
if the element's class list contains the class.import {useEffect, useRef} from 'react'; export default function App() { const ref = useRef(null); // ๐๏ธ Check if the element contains a class on mount useEffect(() => { if (ref.current.classList.contains('my-class')) { console.log('Element contains class'); } else { console.log('Element does NOT contain class'); } }, []); // ๐๏ธ Check if the element contains a class on click const handleClick = event => { if (event.currentTarget.classList.contains('my-class')) { console.log('Element contains class'); } else { console.log('Element does NOT contain class'); } }; return ( <div> <div ref={ref} className="my-class" onClick={handleClick}> Hello world </div> </div> ); }
The code snippet shows how to check if an element contains a class when the component is mounted or when the element is clicked.
The useRef() hook can be passed an
initial value as an argument. The hook returns a mutable ref object whose
.current
property is initialized to the passed argument.
const ref = useRef(null);
current
property on the ref object to get access to the div
element on which we set the ref
prop.// ๐๏ธ Check if the element contains a class on mount useEffect(() => { if (ref.current.classList.contains('my-class')) { console.log('Element contains class'); } else { console.log('Element does NOT contain class'); } }, []);
When we pass a ref prop to an element, e.g. <div ref={myRef} />
, React sets
the .current
property of the ref object to the corresponding DOM node.
We passed an empty dependencies array to the useEffect hook, so it's only going to run when the component mounts.
The
classList.contains
method returns a boolean value - true
if the class is contained in the
element's class list and false
otherwise.
If you need to add or remove a class on click, check out the following tutorial.
If you need to check if an element contains a class on click, set the onClick
prop on the element.
To check if an element contains a class on click in React.js:
onClick
prop on the element.classList.contains()
method on the event.target
object to check
if the element contains the class.import {useRef} from 'react'; export default function App() { const ref = useRef(null); // ๐๏ธ Check if an element contains class on click const handleClick = event => { if (event.currentTarget.classList.contains('my-class')) { console.log('Element contains class'); } else { console.log('Element does NOT contain class'); } }; return ( <div> <div ref={ref} className="my-class" onClick={handleClick} style={{backgroundColor: 'lime'}} > bobbyhadz.com </div> </div> ); }
We set the onClick
prop on the div
element, so the handleClick
function
gets invoked every time the element is clicked.
const handleClick = event => { if (event.currentTarget.classList.contains('my-class')) { console.log('Element contains class'); } else { console.log('Element does NOT contain class'); } };
currentTarget
property on the event because we want to access the element that the event listener is attached to.The target
property on the event
gives us a reference to the element that
triggered the event (could be a descendant).
In our handleClick
method, we again use the classList.contains()
method to
check if the element contains the class.