Responsive design with Hooks

In web apps, it is often important to have a responsive design. Responsive design makes your web app render well on various devices and window/screen sizes. Our blog app might be viewed on a desktop, a mobile phone, a tablet, or maybe even a very large screen, such as a TV.

Often, it makes the most sense to simply use CSS media queries for responsive design. However, sometimes that is not possible, for example, when we render elements within a canvas or Web Graphics Library (WebGL). Sometimes, we also want to use the window size in order to decide whether to load a component or not, instead of simply rendering it and then hiding it via CSS later.

The @rehooks/window-size library provides the useWindowSize Hook, which returns the following values:

  • innerWidth: Equal to the window.innerWidth value
  • innerHeight: Equal to the window.innerHeight value
  • outerWidth: Equal to the window.outerWidth value
  • outerHeight: Equal to the window.outerHeight value

To show the difference between outerWidth/outerHeight, and innerWidth/innerHeight, take a look at the following diagram:

Visualization of the window width/height properties

As we can see, innerHeight and innerWidth specify the innermost part of the browser window, while outerHeight and outerWidth specify the full dimensions of the browser window, including the URL bar, scroll bars, and so on.

We are now going to hide components based on the window size in our blog app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.186.201