fix(usePrevious): opt out usePrevious hook from React Compiler#379
Open
lumirlumir wants to merge 3 commits into
Open
fix(usePrevious): opt out usePrevious hook from React Compiler#379lumirlumir wants to merge 3 commits into
usePrevious hook from React Compiler#379lumirlumir wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: dea2eed The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Pull request overview
Opt out the usePrevious hook from React Compiler memoization to prevent babel-plugin-react-compiler (with non-none panicThreshold) from erroring on intentional useRef().current access/updates during render.
Changes:
- Add the
'use no memo'directive tousePreviousto disable React Compiler handling for this hook. - Add an inline comment explaining why the directive is needed.
Opt out the `usePrevious` hook from React Compiler to prevent issues.
Clarify comment about React refs rules in usePrevious hook.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds the
'use no memo'directive to theusePrevioushook to prevent React Compiler from throwing whenpanicThresholdis set to anything other than'none'.For example, without this directive, using this hook with
babel-plugin-react-compilerandpanicThreshold: 'critical_errors'throws the following error:Error Log
ReactCompilerError: Found 8 errors: Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 23 | const currentRef = useRef(state); 24 | const isFirstRender = useRef(true); > 25 | if (isFirstRender.current) { | ^^^^^^^^^^^^^^^^^^^^^ Cannot access ref value during render 26 | isFirstRender.current = false; 27 | return prevRef.current; 28 | } Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 24 | const isFirstRender = useRef(true); 25 | if (isFirstRender.current) { > 26 | isFirstRender.current = false; | ^^^^^^^^^^^^^^^^^^^^^ Cannot update ref during render 27 | return prevRef.current; 28 | } 29 | if (!compare(currentRef.current, state)) { Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 25 | if (isFirstRender.current) { 26 | isFirstRender.current = false; > 27 | return prevRef.current; | ^^^^^^^^^^^^^^^ Cannot access ref value during render 28 | } 29 | if (!compare(currentRef.current, state)) { 30 | prevRef.current = currentRef.current; Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 27 | return prevRef.current; 28 | } > 29 | if (!compare(currentRef.current, state)) { | ^^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render 30 | prevRef.current = currentRef.current; 31 | currentRef.current = state; 32 | } Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 28 | } 29 | if (!compare(currentRef.current, state)) { > 30 | prevRef.current = currentRef.current; | ^^^^^^^^^^^^^^^ Cannot update ref during render 31 | currentRef.current = state; 32 | } 33 | return prevRef.current; Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 28 | } 29 | if (!compare(currentRef.current, state)) { > 30 | prevRef.current = currentRef.current; | ^^^^^^^^^^^^^^^^^^ Cannot access ref value during render 31 | currentRef.current = state; 32 | } 33 | return prevRef.current; Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 29 | if (!compare(currentRef.current, state)) { 30 | prevRef.current = currentRef.current; > 31 | currentRef.current = state; | ^^^^^^^^^^^^^^^^^^ Cannot update ref during render 32 | } 33 | return prevRef.current; 34 | } Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). 31 | currentRef.current = state; 32 | } > 33 | return prevRef.current; | ^^^^^^^^^^^^^^^ Cannot access ref value during render 34 | } 35 |As this is simple directive addition, and currently
babel-plugin-react-compilersetup is not set in this repo, I've skipped the test on it.Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?