Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

on window resize react

import React, { useLayoutEffect, useState } from 'react';

function useWindowSize() {
  const [size, setSize] = useState([0, 0]);
  useLayoutEffect(() => {
    function updateSize() {
      setSize([window.innerWidth, window.innerHeight]);
    }
    window.addEventListener('resize', updateSize);
    updateSize();
    return () => window.removeEventListener('resize', updateSize);
  }, []);
  return size;
}

function ShowWindowDimensions(props) {
  const [width, height] = useWindowSize();
  return <span>Window size: {width} x {height}</span>;
}
Comment

window size change react

const [dimensions, setDimensions] = useState({
    height: typeof window !== 'undefined' && window.innerHeight,
    width: typeof window !== 'undefined' && window.innerWidth,
  });
useEffect(() => {
    const handleResize = () => {
      setDimensions({
        height: window.innerHeight,
        width: window.innerWidth,
      });
    };
    window.addEventListener('resize', handleResize);
  }, []);

return {
      {console.log(`Rendered at ${dimensions.width} x ${dimensions.height}`)}
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: span vertical align 
Javascript :: checkbox on click jquery select all 
Javascript :: jquery smooth scrool 
Javascript :: javascript get first 2 char 
Javascript :: track window resize in vue 
Javascript :: internal/modules/cjs/loader.js:905 throw err; 
Javascript :: include node_modules from search vscode 
Javascript :: node version not specified in package.json 
Javascript :: how to drawImage on center of canvas 
Javascript :: js onsubmit prevent default 
Javascript :: json server sorting 
Javascript :: how to check element has an attribute js 
Javascript :: remove last character from string js 
Javascript :: check email js 
Javascript :: js letters alphabet array 
Javascript :: vscode auto import single quote 
Javascript :: get page resolution jquery 
Javascript :: primera letra en mayuscula javascript 
Javascript :: javascript string into substrings of length 
Javascript :: npm react router dom@5 
Javascript :: javascript fetch api post 
Javascript :: remove word from string javascript 
Javascript :: jquery get all checked checkboxes 
Javascript :: random id generator 
Javascript :: replace an element with another jquery 
Javascript :: Typography material ui import 
Javascript :: jstl replace 
Javascript :: setinterval nodejs 
Javascript :: get button text javascript 
Javascript :: jquery get image src 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =