Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to get window size in react js

const Component = () => {
  const { height, width } = useWindowDimensions();

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

how to get window size in react js

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

PREVIOUS NEXT
Code Example
Javascript :: javascript convert int to float with 2 decimal places 
Javascript :: document load complete jquery 
Javascript :: jquery on 2 events 
Javascript :: javascript reverse array without modifying 
Javascript :: jquery datatable get data array 
Javascript :: javascript click to copy 
Javascript :: stop next script when ajaxcall 
Javascript :: js exec iterate all matches 
Javascript :: js after now 
Javascript :: iterating 3x3x3 array 
Javascript :: useeffect not working with react-dom-router 
Javascript :: mlutiple css jquery 
Javascript :: how to hide mouse pointer in javascript 
Javascript :: findmany mongoose or find by multiple Ids 
Javascript :: button click javascript 
Javascript :: js round up decimal 
Javascript :: how to append more elements after click in react 
Javascript :: javascript code to refresh page automatically 
Javascript :: js pixelated 
Javascript :: react router add fallback to catch all 
Javascript :: byte to kb javascript 
Javascript :: mousemove jquery 
Javascript :: How to install express package using npm 
Javascript :: __dirname go back one directory 
Javascript :: javascript compare 2 thresholds color 
Javascript :: vscode linux launch.json file cpp 
Javascript :: enable input jquery 
Javascript :: store array in local storage js 
Javascript :: use set to remove duplicates in javascript 
Javascript :: htmlparser2 extract text from html 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =