GeezerVison is showing github.com — [ReactDebugTools] add custom error type for future new hooks (#24168) · react/react@0415b18
File tree
Expand file treeCollapse file tree
Open diff view settings
packages/react-debug-tools/src
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/ReactDebugHooks.js +19-2Lines changed: 19 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -356,6 +356,23 @@ const Dispatcher: DispatcherType = { |
356 | 356 | useId, |
357 | 357 | }; |
358 | 358 |
|
| 359 | +// create a proxy to throw a custom error |
| 360 | +// in case future versions of React adds more hooks |
| 361 | +const DispatcherProxyHandler = { |
| 362 | + get(target, prop) { |
| 363 | + if (target.hasOwnProperty(prop)) { |
| 364 | + return target[prop]; |
| 365 | + } |
| 366 | + const error = new Error('Missing method in Dispatcher: ' + prop); |
| 367 | + // Note: This error name needs to stay in sync with react-devtools-shared |
| 368 | + // TODO: refactor this if we ever combine the devtools and debug tools packages |
| 369 | + error.name = 'UnsupportedFeatureError'; |
| 370 | + throw error; |
| 371 | + }, |
| 372 | +}; |
| 373 | + |
| 374 | +const DispatcherProxy = new Proxy(Dispatcher, DispatcherProxyHandler); |
| 375 | + |
359 | 376 | // Inspect |
360 | 377 |
|
361 | 378 | export type HookSource = { |
@@ -664,7 +681,7 @@ export function inspectHooks<Props>( |
664 | 681 |
|
665 | 682 | const previousDispatcher = currentDispatcher.current; |
666 | 683 | let readHookLog; |
667 | | - currentDispatcher.current = Dispatcher; |
| 684 | + currentDispatcher.current = DispatcherProxy; |
668 | 685 | let ancestorStackError; |
669 | 686 | try { |
670 | 687 | ancestorStackError = new Error(); |
@@ -708,7 +725,7 @@ function inspectHooksOfForwardRef<Props, Ref>( |
708 | 725 | ): HooksTree { |
709 | 726 | const previousDispatcher = currentDispatcher.current; |
710 | 727 | let readHookLog; |
711 | | - currentDispatcher.current = Dispatcher; |
| 728 | + currentDispatcher.current = DispatcherProxy; |
712 | 729 | let ancestorStackError; |
713 | 730 | try { |
714 | 731 | ancestorStackError = new Error(); |
|