diff --git a/packages/react-debug-tools/src/ReactDebugHooks.js b/packages/react-debug-tools/src/ReactDebugHooks.js index 2d8f8647ad65c..fbff47bb722fe 100644 --- a/packages/react-debug-tools/src/ReactDebugHooks.js +++ b/packages/react-debug-tools/src/ReactDebugHooks.js @@ -55,7 +55,7 @@ function getPrimitiveStackCache(): Map> { Dispatcher.useLayoutEffect(() => {}); Dispatcher.useEffect(() => {}); Dispatcher.useImperativeHandle(undefined, () => null); - Dispatcher.useDebugValueLabel(null); + Dispatcher.useDebugValue(null); Dispatcher.useCallback(() => {}); Dispatcher.useMemo(() => null); } finally { @@ -181,9 +181,9 @@ function useImperativeHandle( }); } -function useDebugValueLabel(valueLabel: any) { +function useDebugValue(valueLabel: any) { hookLog.push({ - primitive: 'DebugValueLabel', + primitive: 'DebugValue', stackError: new Error(), value: valueLabel, }); @@ -214,12 +214,8 @@ const Dispatcher = { useCallback, useContext, useEffect, -<<<<<<< HEAD useImperativeHandle, -======= - useImperativeMethods, - useDebugValueLabel, ->>>>>>> Support custom values for custom hooks + useDebugValue, useLayoutEffect, useMemo, useReducer, @@ -418,19 +414,19 @@ function buildTree(rootStack, readHookLog): HooksTree { } // Associate custom hook values (useInpect() hook entries) with the correct hooks - rootChildren.forEach(hooksNode => rollupDebugValueLabels(hooksNode)); + rootChildren.forEach(hooksNode => rollupDebugValues(hooksNode)); return rootChildren; } -function rollupDebugValueLabels(hooksNode: HooksNode): void { +function rollupDebugValues(hooksNode: HooksNode): void { let useInpectHooksNodes: Array = []; hooksNode.subHooks = hooksNode.subHooks.filter(subHooksNode => { - if (subHooksNode.name === 'DebugValueLabel') { + if (subHooksNode.name === 'DebugValue') { useInpectHooksNodes.push(subHooksNode); return false; } else { - rollupDebugValueLabels(subHooksNode); + rollupDebugValues(subHooksNode); return true; } }); diff --git a/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.internal.js b/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.internal.js index 47e8fc0a3b5ab..6b61fcf9a4c6e 100644 --- a/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.internal.js +++ b/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.internal.js @@ -41,7 +41,7 @@ describe('ReactHooksInspection', () => { it('should inspect a simple custom hook', () => { function useCustom(value) { let [state] = React.useState(value); - React.useDebugValueLabel('custom hook label'); + React.useDebugValue('custom hook label'); return state; } function Foo(props) { @@ -52,7 +52,7 @@ describe('ReactHooksInspection', () => { expect(tree).toEqual([ { name: 'Custom', - value: 'custom hook label', + value: __DEV__ ? 'custom hook label' : undefined, subHooks: [ { name: 'State', diff --git a/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.internal.js b/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.internal.js index a117b1e269b33..2ecb64a4e8255 100644 --- a/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.internal.js +++ b/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.internal.js @@ -212,11 +212,11 @@ describe('ReactHooksInspectionIntergration', () => { ]); }); - describe('useDebugValueLabel', () => { + describe('useDebugValue', () => { it('should support inspectable values for multiple custom hooks', () => { function useLabeledValue(label) { let [value] = React.useState(label); - React.useDebugValueLabel(`custom label ${label}`); + React.useDebugValue(`custom label ${label}`); return value; } function useAnonymous(label) { @@ -232,14 +232,11 @@ describe('ReactHooksInspectionIntergration', () => { } let renderer = ReactTestRenderer.create(); let childFiber = renderer.root.findByType(Example)._currentFiber(); - let tree = ReactDebugTools.inspectHooksOfFiber( - currentDispatcher, - childFiber, - ); + let tree = ReactDebugTools.inspectHooksOfFiber(childFiber); expect(tree).toEqual([ { name: 'LabeledValue', - value: 'custom label a', + value: __DEV__ ? 'custom label a' : undefined, subHooks: [{name: 'State', value: 'a', subHooks: []}], }, { @@ -254,7 +251,7 @@ describe('ReactHooksInspectionIntergration', () => { }, { name: 'LabeledValue', - value: 'custom label d', + value: __DEV__ ? 'custom label d' : undefined, subHooks: [{name: 'State', value: 'd', subHooks: []}], }, ]); @@ -262,10 +259,11 @@ describe('ReactHooksInspectionIntergration', () => { it('should support inspectable values for nested custom hooks', () => { function useInner() { - React.useDebugValueLabel('inner'); + React.useDebugValue('inner'); + React.useState(0); } function useOuter() { - React.useDebugValueLabel('outer'); + React.useDebugValue('outer'); useInner(); } function Example(props) { @@ -274,27 +272,32 @@ describe('ReactHooksInspectionIntergration', () => { } let renderer = ReactTestRenderer.create(); let childFiber = renderer.root.findByType(Example)._currentFiber(); - let tree = ReactDebugTools.inspectHooksOfFiber( - currentDispatcher, - childFiber, - ); + let tree = ReactDebugTools.inspectHooksOfFiber(childFiber); expect(tree).toEqual([ { name: 'Outer', - value: 'outer', - subHooks: [{name: 'Inner', value: 'inner', subHooks: []}], + value: __DEV__ ? 'outer' : undefined, + subHooks: [ + { + name: 'Inner', + value: __DEV__ ? 'inner' : undefined, + subHooks: [{name: 'State', value: 0, subHooks: []}], + }, + ], }, ]); }); it('should support multiple inspectable values per custom hooks', () => { function useMultiLabelCustom() { - React.useDebugValueLabel('one'); - React.useDebugValueLabel('two'); - React.useDebugValueLabel('three'); + React.useDebugValue('one'); + React.useDebugValue('two'); + React.useDebugValue('three'); + React.useState(0); } function useSingleLabelCustom(value) { - React.useDebugValueLabel(`single ${value}`); + React.useDebugValue(`single ${value}`); + React.useState(0); } function Example(props) { useSingleLabelCustom('one'); @@ -304,25 +307,22 @@ describe('ReactHooksInspectionIntergration', () => { } let renderer = ReactTestRenderer.create(); let childFiber = renderer.root.findByType(Example)._currentFiber(); - let tree = ReactDebugTools.inspectHooksOfFiber( - currentDispatcher, - childFiber, - ); + let tree = ReactDebugTools.inspectHooksOfFiber(childFiber); expect(tree).toEqual([ { name: 'SingleLabelCustom', - value: 'single one', - subHooks: [], + value: __DEV__ ? 'single one' : undefined, + subHooks: [{name: 'State', value: 0, subHooks: []}], }, { name: 'MultiLabelCustom', - value: ['one', 'two', 'three'], - subHooks: [], + value: __DEV__ ? ['one', 'two', 'three'] : undefined, + subHooks: [{name: 'State', value: 0, subHooks: []}], }, { name: 'SingleLabelCustom', - value: 'single two', - subHooks: [], + value: __DEV__ ? 'single two' : undefined, + subHooks: [{name: 'State', value: 0, subHooks: []}], }, ]); }); diff --git a/packages/react-reconciler/src/ReactFiberDispatcher.js b/packages/react-reconciler/src/ReactFiberDispatcher.js index 806c543fd0e23..ed2ed3b2f315b 100644 --- a/packages/react-reconciler/src/ReactFiberDispatcher.js +++ b/packages/react-reconciler/src/ReactFiberDispatcher.js @@ -13,7 +13,7 @@ import { useContext, useEffect, useImperativeHandle, - useDebugValueLabel, + useDebugValue, useLayoutEffect, useMemo, useReducer, @@ -27,7 +27,7 @@ export const Dispatcher = { useContext, useEffect, useImperativeHandle, - useDebugValueLabel, + useDebugValue, useLayoutEffect, useMemo, useReducer, diff --git a/packages/react-reconciler/src/ReactFiberHooks.js b/packages/react-reconciler/src/ReactFiberHooks.js index 71346a3b25cea..f2be782798fc1 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.js +++ b/packages/react-reconciler/src/ReactFiberHooks.js @@ -588,7 +588,7 @@ export function useImperativeHandle( }, nextInputs); } -export function useDebugValueLabel(valueLabel: string): void { +export function useDebugValue(valueLabel: string): void { // This hook is normally a no-op. // The react-debug-hooks package injects its own implementation // so that e.g. DevTools can display customhook values. diff --git a/packages/react/src/React.js b/packages/react/src/React.js index 11821bcb10c73..4b868feca9c88 100644 --- a/packages/react/src/React.js +++ b/packages/react/src/React.js @@ -33,7 +33,7 @@ import { useContext, useEffect, useImperativeHandle, - useDebugValueLabel, + useDebugValue, useLayoutEffect, useMemo, useReducer, @@ -99,12 +99,8 @@ if (enableHooks) { React.useCallback = useCallback; React.useContext = useContext; React.useEffect = useEffect; -<<<<<<< HEAD React.useImperativeHandle = useImperativeHandle; -======= - React.useImperativeMethods = useImperativeMethods; - React.useDebugValueLabel = useDebugValueLabel; ->>>>>>> Support custom values for custom hooks + React.useDebugValue = useDebugValue; React.useLayoutEffect = useLayoutEffect; React.useMemo = useMemo; React.useReducer = useReducer; diff --git a/packages/react/src/ReactHooks.js b/packages/react/src/ReactHooks.js index 6f15eb682fea1..bd4f9c5dfc320 100644 --- a/packages/react/src/ReactHooks.js +++ b/packages/react/src/ReactHooks.js @@ -111,7 +111,9 @@ export function useImperativeHandle( return dispatcher.useImperativeHandle(ref, create, inputs); } -export function useDebugValueLabel(valueLabel: string) { - const dispatcher = resolveDispatcher(); - return dispatcher.useDebugValueLabel(valueLabel); +export function useDebugValue(valueLabel: string) { + if (__DEV__) { + const dispatcher = resolveDispatcher(); + return dispatcher.useDebugValue(valueLabel); + } }