Skip to content

Instantly share code, notes, and snippets.

@ilxanlar
Created June 16, 2021 09:26
Show Gist options
  • Save ilxanlar/c79fc4fcc39157f12f4570a025a729bd to your computer and use it in GitHub Desktop.
Save ilxanlar/c79fc4fcc39157f12f4570a025a729bd to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from 'react'
function Example() {
const [width, setWidth] = useState(...)
useEffect(() => {
function handleResize(event) {
// Calculate window width...
setWidth(...)
}
window.addEventListener('resize', handleResize)
// Specify how to clean up after this effect:
return () => {
window.removeEventListener('resize', handleResize)
}
}, []) // Pass empty array as dependencies
return (
<>
...
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment