@samitouri / QOS-React-2 / commits / 0415b18a10

[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

Mengdi "Monday" Chen committed Mar 30, 2022 at 11:07 UTC 0415b18a100043df164bcd40639f9373e3358350
1 file changed +19 -2
packages/react-debug-tools/src/ReactDebugHooks.js
+19 -2
@@ -356,6 +356,23 @@ const Dispatcher: DispatcherType = {
356 useId,
357 };
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 +
376 // Inspect
377
378 export type HookSource = {
@@ -664,7 +681,7 @@ export function inspectHooks<Props>(
681
682 const previousDispatcher = currentDispatcher.current;
683 let readHookLog;
667 - currentDispatcher.current = Dispatcher;
684 + currentDispatcher.current = DispatcherProxy;
685 let ancestorStackError;
686 try {
687 ancestorStackError = new Error();
@@ -708,7 +725,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
725 ): HooksTree {
726 const previousDispatcher = currentDispatcher.current;
727 let readHookLog;
711 - currentDispatcher.current = Dispatcher;
728 + currentDispatcher.current = DispatcherProxy;
729 let ancestorStackError;
730 try {
731 ancestorStackError = new Error();