Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react get dynamic window sizes

import { useState, useEffect } from 'react';

function getWindowDimensions() {
  const { innerWidth: width, innerHeight: height } = window;
  return {
    width,
    height
  };
}

export default function useWindowDimensions() {
  const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions());

  useEffect(() => {
    function handleResize() {
      setWindowDimensions(getWindowDimensions());
    }

    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);

  return windowDimensions;
}

// Adapted answer from https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs
// Best suited for using in SPA.
Comment

PREVIOUS NEXT
Code Example
Javascript :: express serve node module 
Javascript :: divide string in lines react native max width 
Javascript :: auto closing not working jsx 
Javascript :: does pycharm community edition support javascript 
Javascript :: javascript to typescript converter online 
Javascript :: country name based on browser 
Javascript :: javascript source code for digital meter 
Javascript :: Add rows to the table dynamically with the use of vue.js 
Javascript :: expressions meaning in bengali 
Javascript :: how to define an object in javascript 
Javascript :: livewire afterDomUpdate 
Javascript :: modal nodejs 
Javascript :: Pause interval button javascript 
Javascript :: discord.js add image to embed 
Javascript :: serve file nodejs ubuntu 
Javascript :: read data from store i ngrxstore 
Javascript :: discord js send author a dm 
Javascript :: how to get mobile preferences is it dark or light using javascript 
Javascript :: dropzone alert 
Javascript :: how to create hexadecimal encoded files in javascript 
Javascript :: node.js version change to 6.14.15 windows 
Javascript :: createelement and set prop attr jquery 
Javascript :: javascript regex tester online 
Javascript :: manifest.json background service worker vs scripts 
Javascript :: use variable in form action vuejs 
Javascript :: ityped extension for react 
Javascript :: undo pwa 
Javascript :: online javascript coding test 
Javascript :: nvm install a particular version 
Javascript :: jeebisah 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =