From f50daa5ff9ad641ac3a1b469df7255b53142b176 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 21 Jan 2019 20:34:38 +0000 Subject: [PATCH] Revert "Double-render function components with Hooks in DEV in StrictMode" (#14652) * Revert "Revert "Disallow reading context during useMemo etc" (#14651)" This reverts commit 5fce6488ce1dc97e31515a47ff409d32ab722d65. * Revert "Add test coverage for readContext() on the server (#14649)" This reverts commit fe2ecd276e9b9e57a49ddf2d86ff01677b69b493. * Revert "Warn about incorrect use of useImperativeHandle() (#14647)" This reverts commit 8f45a7fdc469c6d88ca5ca105095b03dd4e862e1. * Revert "Disallow reading context during useMemo etc (#14648)" This reverts commit 1fcbd22431d97e404c1f684744dc543b6c22b076. * Revert "Warn about refs on lazy function components (#14645)" This reverts commit 2a084f51a94df30e3bffd42bf3a04ce3da231512. * Revert "Fix typo (#14560)" This reverts commit b5a3df6e8894ebf2334179e445b0803f6819aa9f. * Revert "fix typo (#14316)" This reverts commit 9c146e675174a523f90f630c87f62f55812bacea. * Revert "Mention forwardRef() in errors and warnings (#14644)" This reverts commit baa6d40fc856c65e1d74ddf4fa038d7a65a787ed. * Revert "Double-render function components with Hooks in DEV in StrictMode (#14643)" This reverts commit a1414e8949915c006bba411fa2949b304dd044ae. --- .../src/ReactFiberBeginWork.js | 53 ----- .../src/__tests__/ReactHooks-test.internal.js | 220 ------------------ 2 files changed, 273 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberBeginWork.js b/packages/react-reconciler/src/ReactFiberBeginWork.js index 9c4a72b7fedcb..5fc0fcc871426 100644 --- a/packages/react-reconciler/src/ReactFiberBeginWork.js +++ b/packages/react-reconciler/src/ReactFiberBeginWork.js @@ -250,23 +250,6 @@ function updateForwardRef( ref, renderExpirationTime, ); - if ( - debugRenderPhaseSideEffects || - (debugRenderPhaseSideEffectsForStrictMode && - workInProgress.mode & StrictMode) - ) { - // Only double-render components with Hooks - if (workInProgress.memoizedState !== null) { - renderWithHooks( - current, - workInProgress, - render, - nextProps, - ref, - renderExpirationTime, - ); - } - } setCurrentPhase(null); } else { nextChildren = renderWithHooks( @@ -560,23 +543,6 @@ function updateFunctionComponent( context, renderExpirationTime, ); - if ( - debugRenderPhaseSideEffects || - (debugRenderPhaseSideEffectsForStrictMode && - workInProgress.mode & StrictMode) - ) { - // Only double-render components with Hooks - if (workInProgress.memoizedState !== null) { - renderWithHooks( - current, - workInProgress, - Component, - nextProps, - context, - renderExpirationTime, - ); - } - } setCurrentPhase(null); } else { nextChildren = renderWithHooks( @@ -1244,25 +1210,6 @@ function mountIndeterminateComponent( } else { // Proceed under the assumption that this is a function component workInProgress.tag = FunctionComponent; - if (__DEV__) { - if ( - debugRenderPhaseSideEffects || - (debugRenderPhaseSideEffectsForStrictMode && - workInProgress.mode & StrictMode) - ) { - // Only double-render components with Hooks - if (workInProgress.memoizedState !== null) { - renderWithHooks( - null, - workInProgress, - Component, - props, - context, - renderExpirationTime, - ); - } - } - } reconcileChildren(null, workInProgress, value, renderExpirationTime); if (__DEV__) { validateFunctionComponentInDev(workInProgress, Component); diff --git a/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js b/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js index 0008a27b14850..1ab149dd94c9b 100644 --- a/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js +++ b/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js @@ -699,224 +699,4 @@ describe('ReactHooks', () => { 'Hooks can only be called inside the body of a function component', ); }); - - it('double-invokes components with Hooks in Strict Mode', () => { - ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = true; - - const {useState, StrictMode} = React; - let renderCount = 0; - - function NoHooks() { - renderCount++; - return
; - } - - function HasHooks() { - useState(0); - renderCount++; - return
; - } - - const FwdRef = React.forwardRef((props, ref) => { - renderCount++; - return
; - }); - - const FwdRefHasHooks = React.forwardRef((props, ref) => { - useState(0); - renderCount++; - return
; - }); - - const Memo = React.memo(props => { - renderCount++; - return
; - }); - - const MemoHasHooks = React.memo(props => { - useState(0); - renderCount++; - return
; - }); - - function Factory() { - return { - state: {}, - render() { - renderCount++; - return
; - }, - }; - } - - let renderer = ReactTestRenderer.create(null); - - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(1); - - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(1); - - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(1); - - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Treated like a class - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Treated like a class - - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Has Hooks - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Has Hooks - - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Has Hooks - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Has Hooks - - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update(); - expect(renderCount).toBe(1); - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Has Hooks - renderCount = 0; - renderer.update( - - - , - ); - expect(renderCount).toBe(__DEV__ ? 2 : 1); // Has Hooks - }); - - it('double-invokes useMemo in DEV StrictMode despite []', () => { - ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = true; - const {useMemo, StrictMode} = React; - - let useMemoCount = 0; - function BadUseMemo() { - useMemo(() => { - useMemoCount++; - }, []); - return
; - } - - useMemoCount = 0; - ReactTestRenderer.create( - - - , - ); - expect(useMemoCount).toBe(__DEV__ ? 2 : 1); // Has Hooks - }); });