Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Applied recent changes to server rendering also
  • Loading branch information
bvaughn committed Jan 26, 2018
commit a3635c928a0c22d6c4d84d0bbd23f86e8f373cd0
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,30 @@ describe('ReactDOMServerLifecycles', () => {
// De-duped
ReactDOMServer.renderToString(<Component />);
});

describe('react-lifecycles-compat', () => {
// TODO Replace this with react-lifecycles-compat once it's been published
function polyfill(Component) {
Component.prototype.componentWillMount = function() {};
Component.prototype.componentWillMount.__suppressDeprecationWarning = true;
Component.prototype.componentWillReceiveProps = function() {};
Component.prototype.componentWillReceiveProps.__suppressDeprecationWarning = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pollutes the react module but there's no resetModules() in afterEach. Let's add it so that any tests added later to this file aren't affected by this monkeypatching.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice call.

}

it('should not warn about deprecated cWM/cWRP for polyfilled components', () => {
class PolyfilledComponent extends React.Component {
state = {};
static getDerivedStateFromProps() {
return null;
}
render() {
return null;
}
}

polyfill(PolyfilledComponent);

ReactDOMServer.renderToString(<PolyfilledComponent />);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('ReactDOMServerLifecycles', () => {
resetModules();
});

it('should invoke the correct lifecycle hooks', () => {
it('should invoke the correct legacy lifecycle hooks', () => {
const log = [];

class Outer extends React.Component {
Expand Down Expand Up @@ -65,6 +65,59 @@ describe('ReactDOMServerLifecycles', () => {
]);
});

it('should invoke the correct new lifecycle hooks', () => {
const log = [];

class Outer extends React.Component {
state = {};
static getDerivedStateFromProps() {
log.push('outer getDerivedStateFromProps');
return null;
}
render() {
log.push('outer render');
return <Inner />;
}
}

class Inner extends React.Component {
state = {};
static getDerivedStateFromProps() {
log.push('inner getDerivedStateFromProps');
return null;
}
render() {
log.push('inner render');
return null;
}
}

ReactDOMServer.renderToString(<Outer />);
expect(log).toEqual([
'outer getDerivedStateFromProps',
'outer render',
'inner getDerivedStateFromProps',
'inner render',
]);
});

it('should not invoke cWM if static gDSFP is present', () => {
class Component extends React.Component {
state = {};
static getDerivedStateFromProps() {
return null;
}
UNSAFE_componentWillMount() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to have a similar test for non-unsafe naming too?

throw Error('unexpected');
}
render() {
return null;
}
}

ReactDOMServer.renderToString(<Component />);
});

it('should update instance.state with value returned from getDerivedStateFromProps', () => {
class Grandparent extends React.Component {
state = {
Expand Down
11 changes: 8 additions & 3 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,10 @@ function resolve(
if (inst.UNSAFE_componentWillMount || inst.componentWillMount) {
if (inst.componentWillMount) {
if (__DEV__) {
if (warnAboutDeprecatedLifecycles) {
if (
warnAboutDeprecatedLifecycles &&
inst.componentWillMount.__suppressDeprecationWarning !== true
) {
const componentName = getComponentName(Component) || 'Unknown';

if (!didWarnAboutDeprecatedWillMount[componentName]) {
Expand All @@ -534,8 +537,10 @@ function resolve(
}
}

inst.componentWillMount();
} else {
if (typeof Component.getDerivedStateFromProps !== 'function') {
inst.componentWillMount();
}
} else if (typeof Component.getDerivedStateFromProps !== 'function') {
inst.UNSAFE_componentWillMount();
}
if (queue.length) {
Expand Down