Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react get screen width

// useWindowDimensions.js

import { useState, useEffect } from 'react';

export default function useWindowDimensions() {

  const hasWindow = typeof window !== 'undefined';

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

  const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions());

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

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

  return windowDimensions;
}

// yourComponent.js

import useWindowDimensions from './hooks/useWindowDimensions';

const Component = () => {
  const { height, width } = useWindowDimensions();
  /* you can also use default values or alias to use only one prop: */
  // const { height: windowHeight = 480 } useWindowDimensions();

  return (
    <div>
      width: {width} ~ height: {height}
    </div>
  );
Comment

react detect screen size

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;
}
Comment

react js get screen size

  console.log(window.innerHeight);
Comment

PREVIOUS NEXT
Code Example
Javascript :: .ajax how to get data from form 
Javascript :: nuxt progress false 
Javascript :: what is the correct json content type 
Javascript :: js for loop array 
Javascript :: Check if user logged in Wordpress through JS 
Javascript :: how to select div js 
Javascript :: javascript set class of element 
Javascript :: jquery force page to reload on viewport resize 
Javascript :: angular delete with body 
Javascript :: typeorm findone subquery 
Javascript :: toast in angular not working 
Javascript :: jquery after 
Javascript :: scrolltop in javascript 
Javascript :: express req body undefined 
Javascript :: node how to stop NodeJS server process 
Javascript :: javascript round to 2 decimals 
Javascript :: manifest.json basic structure 
Javascript :: count value a to b character javascript 
Javascript :: set background color dynamically javascript 
Javascript :: rounding number to x decimals javascript 
Javascript :: check if item not in array node js 
Javascript :: reload react native app 
Javascript :: $(document).ready(function() alert 
Javascript :: check jquery page 
Javascript :: syntax function 
Javascript :: is a letter javascript 
Javascript :: array to object 
Javascript :: difference between react native and react 
Javascript :: javascript copy to clipboard 
Javascript :: get version from package.json 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =