I have a React app where I am hiding/showing an element based on state, and want to do some calculations on that DOM element after the state variable changes.
I've tried using useLayoutEffect, but this still runs before the DOM has updated, so my calculations aren't useful. Is my understanding of useLayoutEffect wrong? Am I using it incorrectly?
Here's what I have
const myComponent = () => {
const elem = useRef()
const [isElemVisible, setIElemVisible] = useState(false)
useLayoutEffect(() => {
// I want this to run AFTER the element is added/removed from the DOM
// Instead this is run before the element is actually modified in the DOM (but the ref itself has changed)
elem.current.getBoundingClientRect() // do something with this object if it exists
}, [elem.current])
return (
<div id="base-element">
{ isElemVisible && (
<div id="element" ref={elem}></div>
)}
</div>
)
}