mirror of
https://github.com/zebrajr/react.git
synced 2026-01-15 12:15:22 +00:00
setState returning null and undefined is no-op on the ShallowRenderer (#12756)
This commit is contained in:
committed by
Brandon Dail
parent
25dda90c1e
commit
0bf24cc83e
@@ -301,6 +301,11 @@ class Updater {
|
||||
partialState = partialState(currentState, publicInstance.props);
|
||||
}
|
||||
|
||||
// Null and undefined are treated as no-ops.
|
||||
if (partialState === null || partialState === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._renderer._newState = {
|
||||
...currentState,
|
||||
...partialState,
|
||||
|
||||
@@ -1305,4 +1305,32 @@ describe('ReactShallowRenderer', () => {
|
||||
'UNSAFE_componentWillUpdate',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should stop the upade when setState returns null or undefined', () => {
|
||||
const log = [];
|
||||
let instance;
|
||||
class Component extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
count: 0,
|
||||
};
|
||||
}
|
||||
render() {
|
||||
log.push('render');
|
||||
instance = this;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
const shallowRenderer = createRenderer();
|
||||
shallowRenderer.render(<Component />);
|
||||
log.length = 0;
|
||||
instance.setState(() => null);
|
||||
instance.setState(() => undefined);
|
||||
instance.setState(null);
|
||||
instance.setState(undefined);
|
||||
expect(log).toEqual([]);
|
||||
instance.setState(state => ({count: state.count + 1}));
|
||||
expect(log).toEqual(['render']);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user