I have an element and I want to calculate its width and then iterate how many times it takes to at least fill the width of the viewport.
The problem is, I can't get the width via the inline JSX render style I'm using. Even if I set it as a dependency, I still get the refWidth is empty error in useEffect.
Is there a way to calculate and render correctly without first adding the element to the return statement, getting its width, and then appending the required number of elements?
CodeSandbox link here
To access the width of an element in inline JSX rendering, you can use the useLayoutEffect hook.
useLayoutEffect
hook ensures that width calculations are done after the element is rendered, resulting in more accurate results.