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

chore: update for react hooks guidelines #110

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Daniel-Cross
Copy link

Updating the user guidelines for React hooks.

@Daniel-Cross Daniel-Cross requested a review from a team as a code owner October 21, 2024 17:57
Copy link
Contributor

@sethkfman sethkfman left a comment

Choose a reason for hiding this comment

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

@Daniel-Cross Thanks for getting these added. These are great.

@MajorLift
Copy link
Contributor

MajorLift commented Nov 22, 2024

Perhaps this is out of scope for this PR, but so long as we're discussing memoization tactics specifically, should we add some general remarks about React Compiler? This seems especially relevant now with the new react compiler beta release that supports react v17-18.

It appears that in general manual memoization will be stripped and replaced by React Compiler, and therefore could be removed altogether eventually, although it doesn't seem like this should affect our current code.

However, an exception to this rule is memoization that is intended to ensure correctness instead of performance benefits. Maybe we should add a guideline to highlight any new additions that fall under this category with a comment, so that we know not to remove them in the future.

It is generally safe to leave manual memoization in your code – the compiler understands it but will compile in its own lower-level form of memoization even for manually memoized code. The compiler does however check its memoization against manual memoization to ensure that we memoized it exactly the same. The reason we need to do this is in case the manual memoization was used for correctness. For example, if a manually memoized value was passed as a dependency to a useEffect call somewhere else and it wasn't memoized in the same way, this might cause over/under firing of effects or even infinite loops.

For this reason, our current recommendation is to leave manual memoization in for existing code, but for new code written after the compiler is added you can omit them.

-- reactwg/react-compiler#16 (comment)

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.

4 participants