0

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 render and componentDidMount methods 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 componentDidMount could 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.

Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155

0 Answers0