Skip to content

Commit 0ea690a

Browse files
committed
PureComponent in Fiber
Passes existing PureComponent tests
1 parent 552033e commit 0ea690a

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/renderers/shared/fiber/ReactFiberClassComponent.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var {
2525
} = require('ReactFiberUpdateQueue');
2626
var { isMounted } = require('ReactFiberTreeReflection');
2727
var ReactInstanceMap = require('ReactInstanceMap');
28+
var shallowEqual = require('shallowEqual');
2829

2930
module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : PriorityLevel) => void) {
3031

@@ -73,6 +74,28 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
7374
},
7475
};
7576

77+
function checkShouldComponentUpdate(workInProgress, oldProps, newProps, newState) {
78+
const updateQueue = workInProgress.updateQueue;
79+
if (oldProps === null || updateQueue && updateQueue.isForced)) {
80+
return true;
81+
}
82+
83+
const instance = workInProgress.stateNode;
84+
if (typeof instance.shouldComponentUpdate === 'function') {
85+
return instance.shouldComponentUpdate(newProps, newState);
86+
}
87+
88+
const type = workInProgress.type;
89+
if (type.prototype && type.prototype.isPureReactComponent) {
90+
return (
91+
!shallowEqual(oldProps, newProps) ||
92+
!shallowEqual(instance.state, newState)
93+
);
94+
}
95+
96+
return true;
97+
}
98+
7699
function adoptClassInstance(workInProgress : Fiber, instance : any) : void {
77100
instance.updater = updater;
78101
workInProgress.stateNode = instance;
@@ -132,13 +155,12 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
132155
// componentWillMount and before this componentWillMount? Probably
133156
// unsupported anyway.
134157

135-
const updateQueue = workInProgress.updateQueue;
136-
137-
// If this completed, we might be able to just reuse this instance.
138-
if (typeof instance.shouldComponentUpdate === 'function' &&
139-
!(updateQueue && updateQueue.isForced) &&
140-
workInProgress.memoizedProps !== null &&
141-
!instance.shouldComponentUpdate(newProps, newState)) {
158+
if (!checkShouldComponentUpdate(
159+
workInProgress,
160+
workInProgress.memoizedProps,
161+
newProps,
162+
newState
163+
)) {
142164
return false;
143165
}
144166

@@ -196,10 +218,12 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
196218
newState = previousState;
197219
}
198220

199-
if (typeof instance.shouldComponentUpdate === 'function' &&
200-
!(updateQueue && updateQueue.isForced) &&
201-
oldProps !== null &&
202-
!instance.shouldComponentUpdate(newProps, newState)) {
221+
if (!checkShouldComponentUpdate(
222+
workInProgress,
223+
oldProps,
224+
newProps,
225+
newState
226+
)) {
203227
// TODO: Should this get the new props/state updated regardless?
204228
return false;
205229
}

0 commit comments

Comments
 (0)