Skip to content

Commit

Permalink
store handleWindowResize with useRef
Browse files Browse the repository at this point in the history
  • Loading branch information
silltho authored and kettanaito committed Oct 5, 2019
1 parent e989fc3 commit 2745dde
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/hooks/useViewportChange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react'
import { useEffect, useRef } from 'react'
import throttle from '@utils/functions/throttle'

/**
Expand All @@ -9,12 +9,17 @@ const useViewportChange = (
callback: () => void,
throttleInterval: number = 70,
) => {
const handleWindowResize = throttle(callback, throttleInterval)
const handleWindowResize = useRef<any>()

useEffect(() => {
handleWindowResize()
window.addEventListener('resize', handleWindowResize)
return () => window.removeEventListener('resize', handleWindowResize)
handleWindowResize.current = throttle(callback, throttleInterval)
})

useEffect(() => {
handleWindowResize.current()
window.addEventListener('resize', handleWindowResize.current)
return () =>
window.removeEventListener('resize', handleWindowResize.current)
}, [])
}

Expand Down

0 comments on commit 2745dde

Please sign in to comment.