From 076460d2ad36e04b2af7f191f5ea3d045eaec790 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 6 Nov 2018 21:45:34 +0000 Subject: [PATCH] Add DEV-only checks for assumption about instance properties --- .../src/ReactFiberCommitWork.js | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.js b/packages/react-reconciler/src/ReactFiberCommitWork.js index 9a0ef664b68f..f22e3efb5d71 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.js @@ -54,6 +54,7 @@ import { import getComponentName from 'shared/getComponentName'; import invariant from 'shared/invariant'; import warningWithoutStack from 'shared/warningWithoutStack'; +import warning from 'shared/warning'; import {NoWork} from './ReactFiberExpirationTime'; import {onCommitUnmount} from './ReactFiberDevToolsHook'; @@ -232,6 +233,22 @@ function commitBeforeMutationLifeCycles( // We could update instance props and state here, // but instead we rely on them being set during last render. // TODO: revisit this when we implement resuming. + if (__DEV__) { + if (finishedWork.type === finishedWork.elementType) { + warning( + instance.props === finishedWork.memoizedProps, + 'Expected instance props to match memoized props before ' + + 'getSnapshotBeforeUpdate. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + warning( + instance.state === finishedWork.memoizedState, + 'Expected instance state to match memoized state before ' + + 'getSnapshotBeforeUpdate. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + } + } const snapshot = instance.getSnapshotBeforeUpdate( finishedWork.elementType === finishedWork.type ? prevProps @@ -352,6 +369,22 @@ function commitLifeCycles( // We could update instance props and state here, // but instead we rely on them being set during last render. // TODO: revisit this when we implement resuming. + if (__DEV__) { + if (finishedWork.type === finishedWork.elementType) { + warning( + instance.props === finishedWork.memoizedProps, + 'Expected instance props to match memoized props before ' + + 'componentDidMount. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + warning( + instance.state === finishedWork.memoizedState, + 'Expected instance state to match memoized state before ' + + 'componentDidMount. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + } + } instance.componentDidMount(); stopPhaseTimer(); } else { @@ -364,6 +397,22 @@ function commitLifeCycles( // We could update instance props and state here, // but instead we rely on them being set during last render. // TODO: revisit this when we implement resuming. + if (__DEV__) { + if (finishedWork.type === finishedWork.elementType) { + warning( + instance.props === finishedWork.memoizedProps, + 'Expected instance props to match memoized props before ' + + 'componentDidUpdate. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + warning( + instance.state === finishedWork.memoizedState, + 'Expected instance state to match memoized state before ' + + 'componentDidUpdate. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + } + } instance.componentDidUpdate( prevProps, prevState, @@ -374,6 +423,22 @@ function commitLifeCycles( } const updateQueue = finishedWork.updateQueue; if (updateQueue !== null) { + if (__DEV__) { + if (finishedWork.type === finishedWork.elementType) { + warning( + instance.props === finishedWork.memoizedProps, + 'Expected instance props to match memoized props before ' + + 'processing the update queue. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + warning( + instance.state === finishedWork.memoizedState, + 'Expected instance state to match memoized state before ' + + 'processing the update queue. This is likely due to a bug in React. ' + + 'Please file an issue.', + ); + } + } // We could update instance props and state here, // but instead we rely on them being set during last render. // TODO: revisit this when we implement resuming.