Building server side rendered application with React and NextJS. I want to know the width of the viewport and distribute it through the whole application via props. Getting the width of the viewport is the easy part:
window.innerWidth;
My problem is the following:
- Only in the
renderandcomponentDidMountmethods is the window object available. - We can't set props in the
render()method because this would make the function impure - Checking to see if the viewport width in
componentDidMountcould work, but now we have already rendered the component.
Is there any way to get window.innerWidth before the render method is triggered?
P.S. the solution doesn't have to be passing it via props per se. It could be another solution which helps tackling this issue.