Skip to content

Commit

Permalink
prevent re-setting event listeners on every resize
Browse files Browse the repository at this point in the history
reference https://reactjs.org/docs/hooks-reference.html#functional-updates

“If your update function returns the exact same value as the current state, the subsequent rerender will be skipped completely.”
  • Loading branch information
KATT committed Sep 25, 2020
1 parent 2db7367 commit a1a4579
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/react-div-100vh/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export function use100vh(): number | null {

function setMeasuredHeight() {
const measuredHeight = measureHeight()
if (height !== measuredHeight) setHeight(measuredHeight)
setHeight(measuredHeight)
}

window.addEventListener('resize', setMeasuredHeight)
return () => window.removeEventListener('resize', setMeasuredHeight)
}, [height, wasRenderedOnClientAtLeastOnce])
}, [wasRenderedOnClientAtLeastOnce])
return wasRenderedOnClientAtLeastOnce ? height : null
}

Expand All @@ -57,10 +57,10 @@ function useWasRenderedOnClientAtLeastOnce() {
] = useState(false)

useEffect(() => {
if (isClient() && !wasRenderedOnClientAtLeastOnce) {
if (isClient()) {
setWasRenderedOnClientAtLeastOnce(true)
}
}, [wasRenderedOnClientAtLeastOnce])
}, [])
return wasRenderedOnClientAtLeastOnce
}

Expand Down

0 comments on commit a1a4579

Please sign in to comment.