mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Fix ReactShallowRenderer not rerendering when calling forceUpdate() (#11439)
* Fix ReactShallowRenderer not rerendering when calling forceUpdate() * Style nit
This commit is contained in:
committed by
Dan Abramov
parent
c2b68dae64
commit
779d23f72e
@@ -147,9 +147,11 @@ class ReactShallowRenderer {
|
||||
// Fallback to previous instance state to support rendering React.cloneElement()
|
||||
const state = this._newState || this._instance.state || emptyObject;
|
||||
|
||||
if (typeof this._instance.shouldComponentUpdate === 'function') {
|
||||
if (
|
||||
typeof this._instance.shouldComponentUpdate === 'function' &&
|
||||
!this._forcedUpdate
|
||||
) {
|
||||
if (
|
||||
this._forcedUpdate ||
|
||||
this._instance.shouldComponentUpdate(props, state, context) === false
|
||||
) {
|
||||
this._instance.context = context;
|
||||
|
||||
@@ -142,6 +142,24 @@ describe('ReactShallowRenderer', () => {
|
||||
expect(scuCounter).toEqual(0);
|
||||
});
|
||||
|
||||
it('should rerender when calling forceUpdate', () => {
|
||||
let renderCounter = 0;
|
||||
class SimpleComponent extends React.Component {
|
||||
render() {
|
||||
renderCounter += 1;
|
||||
return <div />;
|
||||
}
|
||||
}
|
||||
|
||||
const shallowRenderer = createRenderer();
|
||||
shallowRenderer.render(<SimpleComponent />);
|
||||
expect(renderCounter).toEqual(1);
|
||||
|
||||
const instance = shallowRenderer.getMountedInstance();
|
||||
instance.forceUpdate();
|
||||
expect(renderCounter).toEqual(2);
|
||||
});
|
||||
|
||||
it('should shallow render a functional component', () => {
|
||||
function SomeComponent(props, context) {
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user