Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

auto load window on change viewport react

import React from 'react'
function MyComponent() {
  const [dimensions, setDimensions] = React.useState({ 
    height: window.innerHeight,
    width: window.innerWidth
  })
  React.useEffect(() => {
    function handleResize() {
      setDimensions({
        height: window.innerHeight,
        width: window.innerWidth
      })
    
}

    window.addEventListener('resize', handleResize)

    return _ => {
      window.removeEventListener('resize', handleResize)
    
}
  })
  return <div>Rendered at {dimensions.width} x {dimensions.height}</div>
}
Source by www.pluralsight.com #
 
PREVIOUS NEXT
Tagged: #auto #load #window #change #viewport #react
ADD COMMENT
Topic
Name
2+3 =