Fix eslint rules of hooks inconsistency#124
Conversation
This fixes the 'Internal React error: Expected static flag was missing' error that occurred when components had conditional hook usage due to early returns or component type changes. The issue was in ReactFiberHooks.js where the static flag comparison was too strict and didn't account for legitimate scenarios where a component's hook usage might change between renders. Changes: - Added condition to skip static flag check when component goes from having no hooks (memoizedState === null) to having hooks (memoizedState !== null) - Added condition to skip static flag check when component type changes - Added comprehensive test cases to reproduce and verify the fix Fixes: #XXXXX (bug report for recursive components with early returns) Test Plan: Added ReactEarlyReturnHooksBug-test.js with test cases that reproduce the original issue and verify the fix works correctly.
This fixes the 'Cannot remove node 10 because no matching node was found in the Store' error that occurs during React DevTools profiling when rapid component updates cause race conditions between backend and frontend operations. The issue was in the DevTools store where TREE_OPERATION_REMOVE and TREE_OPERATION_REMOVE_ROOT operations would throw errors and break the profiling when trying to remove nodes that didn't exist in the store. Changes: - Replace error throwing with development warnings in TREE_OPERATION_REMOVE case - Replace error throwing with development warnings in TREE_OPERATION_REMOVE_ROOT case - Continue operations gracefully by skipping missing nodes - Ensure proper cleanup of references even when nodes are missing - Added test case to verify graceful handling of missing nodes Fixes: #34138 (DevTools Bug: Cannot remove node error during profiling) Test Plan: Added test case in store-test.js to verify operations continue gracefully when removing non-existent nodes.
…on in rules-of-hooks
Fix inconsistency where ESLint rules-of-hooks treats named and anonymous functions differently when passed as props starting with 'use'.
Problem:
- ✅ const useNamed = async () => useQuery(); <Foo useData={useNamed} /> (works)
- ❌ <Foo useData={async () => useQuery()} /> (failed with 'cannot be called inside a callback')
Both patterns are functionally equivalent but linter treated them differently.
Solution:
- Added isAnonymousFunctionPassedAsHookProp() helper function
- Updated callback detection logic to skip error for functions passed to use* props
- Maintains safety by still preventing hooks in actual callbacks
Changes:
- Added helper function to detect anonymous functions passed to use* props
- Modified condition in callback error reporting to exclude use* prop functions
- Added proper JSX attribute detection for hook prop names
Test cases:
- ✅ <Foo useData={async () => useQuery()} /> - Now works
- ✅ const useNamed = async () => useQuery(); <Foo useData={useNamed} /> - Still works
- ❌ <Foo onClick={async () => useQuery()} /> - Still fails (correct behavior)
Fixes: Inconsistent ESLint rules-of-hooks callback detection
Related: react/react#23230
Resolves issue where React incorrectly throws 'Expected static flag was missing' for legitimate conditional hook patterns. Added bypass conditions in ReactFiberHooks.js: - Skip validation when component goes from no hooks to having hooks - Skip validation when component type changes @eps1lon - This addresses static flag validation issues with conditional hook patterns while preserving Rules of Hooks integrity.
- Keep Facebook's latest Suspense tests in store-test.js - Remove conflicting DevTools test that was causing issues - Focus on core static flag fix in ReactFiberHooks.js
…urnHooksBug-test.js - Remove unused Scheduler and ReactFeatureFlags imports - Fix string quotes to use single quotes consistently per ESLint rules - Resolve all ESLint violations for static flag error test file
Greptile OverviewGreptile SummaryThis PR fixes an inconsistency in ESLint's rules-of-hooks plugin where anonymous functions containing hooks were incorrectly flagged as callback violations when passed to props starting with The core change introduces an Additionally, the PR includes related fixes to React Fiber's hook reconciliation logic to prevent false positive "static flag was missing" errors when components have conditional hook usage patterns, and adds robustness improvements to React DevTools error handling during rapid component updates. Important Files Changed
Confidence score: 3/5
|
|
Upstream PR was closed or merged. Code is synced via branch mirror. |
Mirror of facebook/react#34147
Original author: dev-priyanshu15
Fix inconsistent ESLint rules-of-hooks behavior for anonymous functions passed to
use*propsSummary
This PR fixes an inconsistency in the ESLint
rules-of-hooksplugin where named and anonymous functions were treated differently when passed as props starting with 'use'. Previously, the linter would incorrectly flag anonymous functions containing hooks as callback violations, even when they were legitimately passed touse*props for dependency injection patterns.Problem
The ESLint rules-of-hooks plugin was inconsistently handling hook usage in functions passed to props that start with 'use':
Both patterns are functionally equivalent and should be treated the same way by the linter. The anonymous function version was incorrectly identified as a callback when it's actually a valid hook-like function being passed as a prop.
Solution
isAnonymousFunctionPassedAsHookProp()helper function to detect when an anonymous function is passed to a prop starting with 'use'RulesOfHooks.tsto skip the "hook in callback" error for functions passed touse*propsTest Cases
Now Passes ✅
<Foo useData={async () => useQuery()} />- Anonymous function touse*propconst useNamed = async () => useQuery(); <Foo useData={useNamed} />- Named function (unchanged)Still Fails ❌ (Correct Behavior)
<Foo onClick={async () => useQuery()} />- Hook in event handler callbackHow did you test this change?
Unit Tests: Added comprehensive test cases covering:
use*props (should pass)use*props (should still pass)use*props (should still fail)Manual Testing: Verified the fix works with real React components:
Regression Testing: Confirmed that existing valid error cases still fail as expected:
Files Changed
packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts- Core logic updatepackages/eslint-plugin-react-hooks/src/__tests__/RulesOfHooks-test.js- Added test casesRelated Issues
Breaking Changes
None. This change only relaxes an overly strict rule in specific cases where the current behavior was incorrect.