Skip to content
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

Merged
merged 2 commits into from
Sep 25, 2020
Merged

Small optimizations #45

merged 2 commits into from
Sep 25, 2020

Conversation

KATT
Copy link
Contributor

@KATT KATT commented Sep 25, 2020

  • prevent calling measureHeight() on every render - reference
  • prevent re-setting event listeners on every resize
    reference

    “If your update function returns the exact same value as the current state, the subsequent rerender will be skipped completely.”

@vercel vercel bot temporarily deployed to Preview September 25, 2020 10:23 Inactive
@vercel
Copy link

vercel bot commented Sep 25, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/michaelvasin/react-div-100vh/9rfb72gi0
✅ Preview: https://react-div-100vh-git-fork-katt-master.michaelvasin.vercel.app

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.”

Signed-off-by: KATT <[email protected]>
@KATT KATT changed the title Small tweaks Small optimizations Sep 25, 2020
@@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -32,12 +32,12 @@ export function use100vh(): number | null {

function setMeasuredHeight() {
const measuredHeight = measureHeight()
if (height !== measuredHeight) setHeight(measuredHeight)
setHeight(measuredHeight)
Copy link
Contributor Author

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#functional-updates

If your update function returns the exact same value as the current state, the subsequent rerender will be skipped completely.

}

window.addEventListener('resize', setMeasuredHeight)
return () => window.removeEventListener('resize', setMeasuredHeight)
}, [height, wasRenderedOnClientAtLeastOnce])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skips adding/removing event listener on every resize

Comment on lines 59 to -63
useEffect(() => {
if (isClient() && !wasRenderedOnClientAtLeastOnce) {
if (isClient()) {
setWasRenderedOnClientAtLeastOnce(true)
}
}, [wasRenderedOnClientAtLeastOnce])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvasin mvasin merged commit d961353 into mvasin:master Sep 25, 2020
@mvasin
Copy link
Owner

mvasin commented Sep 25, 2020

Looks good to me, thanks @KATT!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants