Skip to content

Commit 0415b18

Browse files
authored
[ReactDebugTools] add custom error type for future new hooks (#24168)
* [ReactDebugTools] add custom error type for future new hooks * update per review comments * remove unused argument
1 parent 8b95ea2 commit 0415b18

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,23 @@ const Dispatcher: DispatcherType = {
356356
useId,
357357
};
358358

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+
359376
// Inspect
360377

361378
export type HookSource = {
@@ -664,7 +681,7 @@ export function inspectHooks<Props>(
664681

665682
const previousDispatcher = currentDispatcher.current;
666683
let readHookLog;
667-
currentDispatcher.current = Dispatcher;
684+
currentDispatcher.current = DispatcherProxy;
668685
let ancestorStackError;
669686
try {
670687
ancestorStackError = new Error();
@@ -708,7 +725,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
708725
): HooksTree {
709726
const previousDispatcher = currentDispatcher.current;
710727
let readHookLog;
711-
currentDispatcher.current = Dispatcher;
728+
currentDispatcher.current = DispatcherProxy;
712729
let ancestorStackError;
713730
try {
714731
ancestorStackError = new Error();

0 commit comments

Comments
 (0)