mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Should not re-render as the result of a null state update (Stack)
Stack now passes test added in previous commit.
This commit is contained in:
1 parent
5c927b902f
commit
2af6d7a702
2 files changed
+30
-26
No files matched your search
@@ -1165,25 +1165,22 @@ describe('ReactUpdates', () => {
|
||||
expect(called).toEqual(true);
|
||||
});
|
||||
|
||||
if (ReactDOMFeatureFlags.useFiber) {
|
||||
// TODO: Should we add this feature to stack, too?
|
||||
it('does not re-render if state update is null', () => {
|
||||
let container = document.createElement('div');
|
||||
it('does not re-render if state update is null', () => {
|
||||
let container = document.createElement('div');
|
||||
|
||||
let instance;
|
||||
let ops = [];
|
||||
class Foo extends React.Component {
|
||||
render() {
|
||||
instance = this;
|
||||
ops.push('render');
|
||||
return <div />;
|
||||
}
|
||||
let instance;
|
||||
let ops = [];
|
||||
class Foo extends React.Component {
|
||||
render() {
|
||||
instance = this;
|
||||
ops.push('render');
|
||||
return <div />;
|
||||
}
|
||||
ReactDOM.render(<Foo />, container);
|
||||
}
|
||||
ReactDOM.render(<Foo />, container);
|
||||
|
||||
ops = [];
|
||||
instance.setState(() => null);
|
||||
expect(ops).toEqual([]);
|
||||
});
|
||||
}
|
||||
ops = [];
|
||||
instance.setState(() => null);
|
||||
expect(ops).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -867,8 +867,9 @@ var ReactCompositeComponent = {
|
||||
|
||||
var nextState = this._processPendingState(nextProps, nextContext);
|
||||
var shouldUpdate = true;
|
||||
|
||||
if (!this._pendingForceUpdate) {
|
||||
var prevState = inst.state;
|
||||
shouldUpdate = willReceive || nextState !== prevState;
|
||||
if (inst.shouldComponentUpdate) {
|
||||
if (__DEV__) {
|
||||
shouldUpdate = measureLifeCyclePerf(
|
||||
@@ -944,15 +945,21 @@ var ReactCompositeComponent = {
|
||||
return queue[0];
|
||||
}
|
||||
|
||||
var nextState = Object.assign({}, replace ? queue[0] : inst.state);
|
||||
var nextState = replace ? queue[0] : inst.state;
|
||||
var dontMutate = true;
|
||||
for (var i = replace ? 1 : 0; i < queue.length; i++) {
|
||||
var partial = queue[i];
|
||||
Object.assign(
|
||||
nextState,
|
||||
typeof partial === 'function' ?
|
||||
partial.call(inst, nextState, props, context) :
|
||||
partial
|
||||
);
|
||||
let partialState = typeof partial === 'function' ?
|
||||
partial.call(inst, nextState, props, context) :
|
||||
partial;
|
||||
if (partialState) {
|
||||
if (dontMutate) {
|
||||
dontMutate = false;
|
||||
nextState = Object.assign({}, nextState, partialState);
|
||||
} else {
|
||||
Object.assign(nextState, partialState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nextState;
|
||||
|
||||
Reference in new issue
Block a user