diff --git a/CHANGELOG.md b/CHANGELOG.md index 03004d6..b1b6342 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ - Improved hook path capturing: - Adjusted the stack trace limit to capture up to 25 entries, guaranteeing a minimum of 20 path entries, an improvement from the previous 6 (fixes #30) - Implemented an alternative method for path extraction to cater to scenarios where a function is either unnamed or its name doesn't start with `use` + - Resolved compatibility issues with WhyDidYouRender (fixes #22) + - Fixed an issue where the hooks list doubled in some cases when a component rendered in StrictMode ## 0.7.3 (June 9, 2023) diff --git a/src/publisher/react-integration/dispatcher-trap.ts b/src/publisher/react-integration/dispatcher-trap.ts index b71a431..4668490 100644 --- a/src/publisher/react-integration/dispatcher-trap.ts +++ b/src/publisher/react-integration/dispatcher-trap.ts @@ -34,6 +34,8 @@ type Dispatcher = { useLayoutEffect(create: AnyFn, deps?: any[]): void; useContext(context: ReactContext, ...rest: any[]): any; readContext(context: ReactContext): any; + useRef(): any; + useImperativeHandle(): any; }; type DispatchFn = ((value: any) => any) & { hookIdx?: number; @@ -117,7 +119,7 @@ export function createDispatcherTrap( context: ReactContext | null = null ) { if (currentFiberCollectInfo !== null) { - currentFiberCollectInfo.hooks?.push({ + currentFiberCollectInfo.hooks.push({ name, deps, context, @@ -350,8 +352,10 @@ export function createDispatcherTrap( knownDispatcher.add(dispatcher); // ContextOnlyDispatcher has a single guard function for each hook, - // detecting it by comparing two random hooks for equality - if (dispatcher.useReducer === dispatcher.useState) { + // detecting it by comparing two random hooks for equality; + // Choosed useRef and useImperativeHandle hooks since WDYR doesn't patch them + // more likely RRT & WDYR would functional together + if (dispatcher.useRef === dispatcher.useImperativeHandle) { ignoreDispatcherTransition.add(dispatcher); } // In dev mode InvalidNestedHooksDispatcher* are used, that's the only @@ -477,6 +481,15 @@ export function createDispatcherTrap( currentFiberCollectInfo = null; currentFiberHookIndex = 0; } + // Setting ContextOnlyDispatcher indicates ending of a render - stop collecting hook info, + // otherwise additional rendering in Strict Mode will double the hooks list + else if ( + nextDispatcher !== null && + nextDispatcher.useRef === nextDispatcher.useImperativeHandle + ) { + currentFiberCollectInfo = null; + currentFiberHookIndex = 0; + } }, }); }