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

Pass buildRouter as a function to useState #252

Merged
merged 1 commit into from
Nov 1, 2022

Conversation

HelKyle
Copy link
Contributor

@HelKyle HelKyle commented Jul 8, 2022

No description provided.

Unverified

This user has not yet uploaded their public signing key.
Copy link
Owner

@molefrog molefrog left a comment

Choose a reason for hiding this comment

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

Hi! Sorry that it took me so long to review this, I've been on a long break from doing open-source. This look good, thank you!

@molefrog molefrog merged commit 5599d9d into molefrog:master Nov 1, 2022
@jorrit
Copy link

jorrit commented Nov 2, 2022

Just curious, why is useState without updates better than useRef?

@HansBrende
Copy link
Contributor

@molefrog @HelKyle @jorrit I am also curious about this... why was this change made? Not seeing any rationale to explain the change. Can someone please explain it to me?

@molefrog
Copy link
Owner

molefrog commented Nov 5, 2022

@molefrog @HelKyle @jorrit I am also curious about this... why was this change made? Not seeing any rationale to explain the change. Can someone please explain it to me?

There really isn't any difference in performance, probably the only benefit is that it is shorter than useRef. Plus, it is now (I believe they rewrote this chapter from the docs, it used to be just useRef approach) the recommended way of doing memoized initialisation. See the link from the comment: https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily

So it is mostly a stylistic choice, I'm good with both versions.

Btw, regarding the comment, I think we could make it shorter, e.g. // initialize the router only once, don't think it requires so much attention.

@HansBrende
Copy link
Contributor

@molefrog gotcha, I see. Don't think it is the recommended way necessarily (since the description on how to lazy-init refs is still present in that same section, under "You might also occasionally want to avoid re-creating the useRef() initial value.")

But agreed that it is purely a stylistic choice without much of a performance impact, was just curious what the rationale was. Thanks!

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.

None yet

4 participants