Skip to content

Commit

Permalink
Resolve compatibility issues with WhyDidYouRender (fixes #22) and dou…
Browse files Browse the repository at this point in the history
…bling hooks
  • Loading branch information
lahmatiy committed Aug 15, 2023
1 parent e6c9bfa commit 1bafd99
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 16 additions & 3 deletions src/publisher/react-integration/dispatcher-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Dispatcher = {
useLayoutEffect(create: AnyFn, deps?: any[]): void;
useContext(context: ReactContext<any>, ...rest: any[]): any;
readContext(context: ReactContext<any>): any;
useRef(): any;
useImperativeHandle(): any;
};
type DispatchFn = ((value: any) => any) & {
hookIdx?: number;
Expand Down Expand Up @@ -117,7 +119,7 @@ export function createDispatcherTrap(
context: ReactContext<any> | null = null
) {
if (currentFiberCollectInfo !== null) {
currentFiberCollectInfo.hooks?.push({
currentFiberCollectInfo.hooks.push({
name,
deps,
context,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
},
});
}
Expand Down

0 comments on commit 1bafd99

Please sign in to comment.