From de2356f19c638af2eda665abf3d29fb5860ea56f Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Tue, 3 Jan 2017 08:45:46 -0800 Subject: [PATCH] Pass prevContext param to componentDidUpdate This makes use of an expando property (__reactInternalPrevContext) on the stateNode (instance). A property on the instance was used instead of a Fiber attribute because: 1) Conditional fields on fibers would break monomorphism. 2) Adding to all fibers would bloat types that don't use context. --- scripts/fiber/tests-failing.txt | 3 --- scripts/fiber/tests-passing.txt | 1 + src/renderers/shared/fiber/ReactFiberCommitWork.js | 9 ++++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/scripts/fiber/tests-failing.txt b/scripts/fiber/tests-failing.txt index ebf46e20be283..f09c52be23701 100644 --- a/scripts/fiber/tests-failing.txt +++ b/scripts/fiber/tests-failing.txt @@ -6,9 +6,6 @@ src/addons/__tests__/ReactFragment-test.js * should throw if a plain object even if it is in an owner * should throw if a plain object looks like an old element -src/isomorphic/classic/__tests__/ReactContextValidator-test.js -* should pass previous context to lifecycles - src/renderers/dom/__tests__/ReactDOMProduction-test.js * should throw with an error code in production diff --git a/scripts/fiber/tests-passing.txt b/scripts/fiber/tests-passing.txt index c749ed37ab64d..3dfeb52b47053 100644 --- a/scripts/fiber/tests-passing.txt +++ b/scripts/fiber/tests-passing.txt @@ -153,6 +153,7 @@ src/isomorphic/children/__tests__/sliceChildren-test.js src/isomorphic/classic/__tests__/ReactContextValidator-test.js * should filter out context not in contextTypes * should pass next context to lifecycles +* should pass previous context to lifecycles * should check context types * should check child context types diff --git a/src/renderers/shared/fiber/ReactFiberCommitWork.js b/src/renderers/shared/fiber/ReactFiberCommitWork.js index 429e892e0bef5..586a684bb8103 100644 --- a/src/renderers/shared/fiber/ReactFiberCommitWork.js +++ b/src/renderers/shared/fiber/ReactFiberCommitWork.js @@ -413,7 +413,8 @@ module.exports = function( if (typeof instance.componentDidUpdate === 'function') { const prevProps = current.memoizedProps; const prevState = current.memoizedState; - instance.componentDidUpdate(prevProps, prevState); + const prevContext = instance.__reactInternalPrevContext; + instance.componentDidUpdate(prevProps, prevState, prevContext); } } attachRef(current, finishedWork, instance); @@ -422,6 +423,12 @@ module.exports = function( if (callbackList) { commitCallbacks(finishedWork, callbackList, instance); } + + // Store updated context for prevContext param in next call to componentDidUpdate(). + // Use an expando property on instance rather than Fiber because: + // 1) Conditional fields on fibers would break monomorphism. + // 2) Adding to all fibers would bloat types that don't use context. + instance.__reactInternalPrevContext = instance.context; return; } case HostRoot: {