-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small optimizations #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ export default function Div100vh({ | |
} | ||
|
||
export function use100vh(): number | null { | ||
const [height, setHeight] = useState<number | null>(measureHeight()) | ||
const [height, setHeight] = useState<number | null>(measureHeight) | ||
|
||
const wasRenderedOnClientAtLeastOnce = useWasRenderedOnClientAtLeastOnce() | ||
|
||
|
@@ -32,12 +32,12 @@ export function use100vh(): number | null { | |
|
||
function setMeasuredHeight() { | ||
const measuredHeight = measureHeight() | ||
if (height !== measuredHeight) setHeight(measuredHeight) | ||
setHeight(measuredHeight) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://reactjs.org/docs/hooks-reference.html#functional-updates
|
||
} | ||
|
||
window.addEventListener('resize', setMeasuredHeight) | ||
return () => window.removeEventListener('resize', setMeasuredHeight) | ||
}, [height, wasRenderedOnClientAtLeastOnce]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. skips adding/removing event listener on every resize |
||
}, [wasRenderedOnClientAtLeastOnce]) | ||
return wasRenderedOnClientAtLeastOnce ? height : null | ||
} | ||
|
||
|
@@ -57,10 +57,10 @@ function useWasRenderedOnClientAtLeastOnce() { | |
] = useState(false) | ||
|
||
useEffect(() => { | ||
if (isClient() && !wasRenderedOnClientAtLeastOnce) { | ||
if (isClient()) { | ||
setWasRenderedOnClientAtLeastOnce(true) | ||
} | ||
}, [wasRenderedOnClientAtLeastOnce]) | ||
Comment on lines
59
to
-63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}, []) | ||
return wasRenderedOnClientAtLeastOnce | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://reactjs.org/docs/hooks-reference.html#lazy-initial-state