Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

pageSize useEffect

import { useState, useEffect } from "react";

// Usage
function App() {
  const size = useWindowSize();

  return (
    <div>
      {size.width}px / {size.height}px
    </div>
  );
}

// Hook
function useWindowSize() {
  // Initialize state with undefined width/height so server and client renders match
  // Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
  const [windowSize, setWindowSize] = useState({
    width: undefined,
    height: undefined,
  });

  useEffect(() => {
    // Handler to call on window resize
    function handleResize() {
      // Set window width/height to state
      setWindowSize({
        width: window.innerWidth,
        height: window.innerHeight,
      });
    }

    // Add event listener
    window.addEventListener("resize", handleResize);

    // Call handler right away so state gets updated with initial window size
    handleResize();

    // Remove event listener on cleanup
    return () => window.removeEventListener("resize", handleResize);
  }, []); // Empty array ensures that effect is only run on mount

  return windowSize;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get specific property name with numbers from object in javascript 
Javascript :: html select structure 
Javascript :: auto linting and testing in react tyescript 
Javascript :: xss bypass greater than 
Javascript :: set timeout with no name 
Javascript :: correct code for the { "vars": "local" } 
Javascript :: javascript complex literal 2 
Javascript :: function multiply(a b) a * b javascript 
Javascript :: generate html by javascript 
Javascript :: javascript dom functions 
Javascript :: sort used in price high and low using angular 
Javascript :: Reversing the elements in an array-like object 
Javascript :: draw image inside canvas width 100% 
Javascript :: javascript reduce form object 
Javascript :: FILTER METHOD. IMPORTANT 
Javascript :: javascript How can i do optional function 
Javascript :: google chrome extension v3 react content security policy issue 
Javascript :: angularjs Both ng-model and ng-change on input alter the $scope state - which one takes priority 
Javascript :: How to call keyup function on textbox for every dynamic generated form in Angular8 
Javascript :: Check AngularJS checkbox with Selenium 
Javascript :: Delete a field from Firebase Firestore where the field/key has a period/punctuation (".") - modular v9 JavaScript SDK 
Javascript :: supertest npm send headers node js 
Javascript :: JavaScript delete atray item 
Javascript :: mongodb create index json 
Javascript :: mongo db get child result with array of parent ids 
Javascript :: Importing Ky Module In JavaScript 
Javascript :: angular error handling 
Javascript :: barcode javascript library 
Javascript :: js a || b 
Javascript :: Accessing Function Declared Outside Constructor But Inside Class 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =