I noticed your comment on Equinox's answer:
truly appreciate your answer! Clarified many things. Now wondering if there is something react specific instead of JQuery. – Ziko 1 hour ago
And yes there is a more React specific way that doesn't require JQuery.
Code Explanation
Using React state hook useState() you can create a function to assign a cssClass prop in your component to toggle on your #tDiv element.
In your HTML set className={cssClass} on your #tDiv element so that the cssClass prop value is applied as a class on the element.
Finally, with the React effect hook useEffect() add an event listener on window to call a function that will...
- compare the size of your
#tDiv element
- if
height >= 500px set the cssClass prop to the name of the CSS class to apply on the #tDiv element
- if not unset the
cssClass prop to remove the CSS class that is applied on the #tDiv element
Code Sample
⚡ CodeSandBox available here: https://codesandbox.io/s/stackoverflow-61418731-k3tz3
This is the component:
import React from "react";
import "./styles.css";
export default function App() {
const [cssClass, setCssClass] = React.useState();
React.useEffect(() => {
// triggered function when window is resized
function handleResize() {
// target the #tDiv element
const tDiv = document.querySelector("#tDiv");
// compare the height
if (tDiv.clientHeight >= 500) {
// set `cssClass` prop to toggle on #tDiv
setCssClass("elevated");
} else {
// unset `cssClass` prop to remove "elevated" class on #tDiv
setCssClass(null);
}
}
// add event listener on window.resize
window.addEventListener("resize", handleResize);
// remove event listener when component is destroyed
return _ => window.removeEventListener("resize", handleResize);
});
// `cssClass` will reflect the prop value to toggle CSS class
return (
<div id="tDiv" className={cssClass}>
Resize the height of your browser to see me elevate!
</div>
);
}
And this is the CSS for the elevated class that apply box-shadow:
.elevated {
box-shadow: 10px 10px 5px #888;
}