Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

useWindowsize hook in react

import { useEffect, useState } from 'react'

// Hook
export default function useWindowSize() {
  const [windowSize, setWindowSize] = useState({
    width: window.innerWidth,
    height: window.innerHeight,
  })
  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
Typescript :: ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. 
Typescript :: get query params from url angular 
Typescript :: get a span inside a div with div id javascript 
Typescript :: drop the rows where all elements are missing in a pandas dataframe 
Typescript :: react native navigation current screen 
Typescript :: sort list of objects by attribute java 
Typescript :: adonis load many 
Typescript :: adonis js order by two columns 
Typescript :: how to check if a variable exists in python 
Typescript :: battle cats challenge battle 
Typescript :: latex reduce the space after section and subsection 
Typescript :: typescript show arguments on function call vscode 
Typescript :: ts playground download 
Typescript :: typescript string interpolation 
Typescript :: list of american tanks 
Typescript :: ERROR in ngcc is already running at process with id 8108. If you are running multiple builds in parallel then you should pre-process your node_modules via the command line ngcc tool before starting the builds; 
Typescript :: angle between two points unity 
Typescript :: add correct host key in known_hosts to get rid of this message 
Typescript :: laravel update if exists or create 
Typescript :: angular date to string format 
Typescript :: What were four effects of the War of 1812? 
Typescript :: on select date matpicker angular 
Typescript :: mat stepper dont clickable 
Typescript :: cast string react 
Typescript :: vue router get full string query 
Typescript :: adjust distance of subplots in python 
Typescript :: nx specify dependencies 
Typescript :: regular expression starts and ends with same symbol 
Typescript :: array contains typescript 
Typescript :: what is endurance testing 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =