Skip to content

Commit 52504a3

Browse files
authored
Invoke both legacy and UNSAFE_ lifecycles when both are present (#12134)
* Invoke both legacy and UNSAFE_ lifecycles when both are present This is to support edge cases with eg create-react-class where a mixin defines a legacy lifecycle but the component being created defines an UNSAFE one (or vice versa). I did not warn about this case because the warning would be a bit redundant with the deprecation warning which we will soon be enabling. I could be convinced to change my stance here though. * Added explicit function-type check to SS ReactPartialRenderer
1 parent 28bb35d commit 52504a3

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/ReactShallowRenderer.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ class ReactShallowRenderer {
209209
if (typeof element.type.getDerivedStateFromProps !== 'function') {
210210
this._instance.componentWillMount();
211211
}
212-
} else if (typeof element.type.getDerivedStateFromProps !== 'function') {
212+
}
213+
if (
214+
typeof this._instance.UNSAFE_componentWillMount === 'function' &&
215+
typeof element.type.getDerivedStateFromProps !== 'function'
216+
) {
213217
// In order to support react-lifecycles-compat polyfilled components,
214218
// Unsafe lifecycles should not be invoked for any component with the new gDSFP.
215219
this._instance.UNSAFE_componentWillMount();
@@ -259,7 +263,8 @@ class ReactShallowRenderer {
259263
if (typeof element.type.getDerivedStateFromProps !== 'function') {
260264
this._instance.componentWillReceiveProps(props, context);
261265
}
262-
} else if (
266+
}
267+
if (
263268
typeof this._instance.UNSAFE_componentWillReceiveProps === 'function' &&
264269
typeof element.type.getDerivedStateFromProps !== 'function'
265270
) {
@@ -316,7 +321,8 @@ class ReactShallowRenderer {
316321
if (typeof type.getDerivedStateFromProps !== 'function') {
317322
this._instance.componentWillUpdate(props, state, context);
318323
}
319-
} else if (
324+
}
325+
if (
320326
typeof this._instance.UNSAFE_componentWillUpdate === 'function' &&
321327
typeof type.getDerivedStateFromProps !== 'function'
322328
) {

src/__tests__/ReactShallowRenderer-test.js

+42
Original file line numberDiff line numberDiff line change
@@ -1235,4 +1235,46 @@ describe('ReactShallowRenderer', () => {
12351235
// De-duped
12361236
shallowRenderer.render(<Component />);
12371237
});
1238+
1239+
it('should invoke both deprecated and new lifecycles if both are present', () => {
1240+
const log = [];
1241+
1242+
class Component extends React.Component {
1243+
componentWillMount() {
1244+
log.push('componentWillMount');
1245+
}
1246+
componentWillReceiveProps() {
1247+
log.push('componentWillReceiveProps');
1248+
}
1249+
componentWillUpdate() {
1250+
log.push('componentWillUpdate');
1251+
}
1252+
UNSAFE_componentWillMount() {
1253+
log.push('UNSAFE_componentWillMount');
1254+
}
1255+
UNSAFE_componentWillReceiveProps() {
1256+
log.push('UNSAFE_componentWillReceiveProps');
1257+
}
1258+
UNSAFE_componentWillUpdate() {
1259+
log.push('UNSAFE_componentWillUpdate');
1260+
}
1261+
render() {
1262+
return null;
1263+
}
1264+
}
1265+
1266+
const shallowRenderer = createRenderer();
1267+
shallowRenderer.render(<Component foo="bar" />);
1268+
expect(log).toEqual(['componentWillMount', 'UNSAFE_componentWillMount']);
1269+
1270+
log.length = 0;
1271+
1272+
shallowRenderer.render(<Component foo="baz" />);
1273+
expect(log).toEqual([
1274+
'componentWillReceiveProps',
1275+
'UNSAFE_componentWillReceiveProps',
1276+
'componentWillUpdate',
1277+
'UNSAFE_componentWillUpdate',
1278+
]);
1279+
});
12381280
});

0 commit comments

Comments
 (0)