fix(usePrevious): opt out usePrevious hook from React Compiler#379
fix(usePrevious): opt out usePrevious hook from React Compiler#379lumirlumir wants to merge 4 commits into
usePrevious hook from React Compiler#379Conversation
🦋 Changeset detectedLatest commit: 2db5ed2 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.
|
Hi @lumirlumir — thanks for catching this! Quick note on my side: CI hasn't run on the fork branch yet, so I'll kick off the workflow and merge once it's green. Really appreciate the fix! |
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?