Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});