Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e709e30
Added unsafe_* lifecycles and deprecation warnings
bvaughn Jan 16, 2018
404944e
Ran lifecycle hook codemod over project
bvaughn Jan 16, 2018
bd300b3
Manually migrated CoffeeScript and TypeScript tests
bvaughn Jan 16, 2018
2868176
Added inline note to createReactClassIntegration-test
bvaughn Jan 16, 2018
8679926
Udated NativeMethodsMixin with new lifecycle hooks
bvaughn Jan 16, 2018
64f27d7
Added static getDerivedStateFromProps to ReactPartialRenderer
bvaughn Jan 17, 2018
1047182
Added getDerivedStateFromProps to shallow renderer
bvaughn Jan 17, 2018
035c220
Dedupe and DEV-only deprecation warning in server renderer
bvaughn Jan 17, 2018
8d0e001
Renamed unsafe_* prefix to UNSAFE_* to be more noticeable
bvaughn Jan 17, 2018
b71ca93
Added getDerivedStateFromProps to ReactFiberClassComponent
bvaughn Jan 17, 2018
09c39d0
Warn about UNSAFE_componentWillRecieveProps misspelling
bvaughn Jan 17, 2018
286df77
Added tests to createReactClassIntegration for new lifecycles
bvaughn Jan 17, 2018
b699543
Added warning for stateless functional components with gDSFP
bvaughn Jan 17, 2018
68f2fe7
Added createReactClass test for static gDSFP
bvaughn Jan 18, 2018
2d9f75d
Moved lifecycle deprecation warnings behind (disabled) feature flag
bvaughn Jan 18, 2018
8f125b7
Tidying up
bvaughn Jan 18, 2018
1d3e3d5
Merge branch 'master' into rfc-6
bvaughn Jan 18, 2018
d95ec49
Tweaked warning message wording slightly
bvaughn Jan 18, 2018
b940938
Replaced truthy partialState checks with != null
bvaughn Jan 18, 2018
6cd0a8e
Call getDerivedStateFromProps via .call(null) to prevent type access
bvaughn Jan 18, 2018
7572667
Move shallow-renderer didWarn* maps off the instance
bvaughn Jan 18, 2018
361a2cf
Only call getDerivedStateFromProps if props instance has changed
bvaughn Jan 18, 2018
8178d52
Avoid creating new state object if not necessary
bvaughn Jan 18, 2018
8d67e27
Inject state as a param to callGetDerivedStateFromProps
bvaughn Jan 18, 2018
53770c3
Explicitly warn about uninitialized state before calling getDerivedSt…
bvaughn Jan 18, 2018
3772ee2
Improved wording for deprecation lifecycle warnings
bvaughn Jan 18, 2018
4dfa6e1
Merge branch 'master' into rfc-6
bvaughn Jan 18, 2018
5609031
Fix state-regression for module-pattern components
bvaughn Jan 19, 2018
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
Added static getDerivedStateFromProps to ReactPartialRenderer
Also added a new set of tests focused on server side lifecycle hooks.
  • Loading branch information
bvaughn committed Jan 17, 2018
commit 64f27d79743b21de5fcaf5d67aed416ab45294d0
150 changes: 150 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMServerLifecycles-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOMServer;

function initModules() {
// Reset warning cache.
jest.resetModuleRegistry();
React = require('react');
ReactDOMServer = require('react-dom/server');

// Make them available to the helpers.
return {
ReactDOMServer,
};
}

const {resetModules} = ReactDOMServerIntegrationUtils(initModules);

describe('ReactDOMServerLifecycles', () => {
beforeEach(() => {
resetModules();
});

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

class Outer extends React.Component {
unsafe_componentWillMount() {
log.push('outer componentWillMount');
}
render() {
log.push('outer render');
return <Inner />;
}
}

class Inner extends React.Component {
unsafe_componentWillMount() {
log.push('inner componentWillMount');
}
render() {
log.push('inner render');
return null;
}
}

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

it('should warn about deprecated lifecycle hooks', () => {
class Component extends React.Component {
componentWillMount() {}
render() {
return null;
}
}

expect(() => ReactDOMServer.renderToString(<Component />)).toWarnDev(
'Warning: Component: componentWillMount() is deprecated and will be removed ' +
'in the next major version. Please use unsafe_componentWillMount() instead.',
);
});

it('should update instance.state with value returned from getDerivedStateFromProps', () => {
class Grandparent extends React.Component {
state = {
foo: 'foo',
};
render() {
return (
<div>
{`Grandparent: ${this.state.foo}`}
<Parent />
</div>
);
}
}

class Parent extends React.Component {
state = {
bar: 'bar',
baz: 'baz',
};
static getDerivedStateFromProps(props, prevState) {
return {
bar: `not ${prevState.bar}`,
};
}
render() {
return (
<div>
{`Parent: ${this.state.bar}, ${this.state.baz}`}
<Child />;
</div>
);
}
}

class Child extends React.Component {
static getDerivedStateFromProps() {
return {
qux: 'qux',
};
}
render() {
return `Child: ${this.state.qux}`;
}
}

const markup = ReactDOMServer.renderToString(<Grandparent />);
expect(markup).toContain('Grandparent: foo');
expect(markup).toContain('Parent: not bar, baz');
expect(markup).toContain('Child: qux');
});

it('should warn if getDerivedStateFromProps returns undefined', () => {
class Component extends React.Component {
static getDerivedStateFromProps() {}
render() {
return null;
}
}

expect(() => ReactDOMServer.renderToString(<Component />)).toWarnDev(
'Component.getDerivedStateFromProps(): A valid state object (or null) must ' +
'be returned. You may have returned undefined.',
);

// De-duped
ReactDOMServer.renderToString(<Component />);
});
});
28 changes: 28 additions & 0 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ let didWarnDefaultTextareaValue = false;
let didWarnInvalidOptionChildren = false;
const didWarnAboutNoopUpdateForComponent = {};
const didWarnAboutBadClass = {};
const didWarnAboutUndefinedDerivedState = {};
const valuePropNames = ['value', 'defaultValue'];
const newlineEatingTags = {
listing: true,
Expand Down Expand Up @@ -421,6 +422,33 @@ function resolve(

if (shouldConstruct(Component)) {
inst = new Component(element.props, publicContext, updater);

if (typeof Component.getDerivedStateFromProps === 'function') {
partialState = Component.getDerivedStateFromProps(
element.props,
inst.state,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be null, right? Do we want to pass {} in this case for type safety?

I don't remember what this debate settled on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can be null, yup. I assume this was desirable since it can be used to detect that the state has not yet been initialized. Alternately if you've initialized the state via the constructor it will be whatever that value us.

);

if (__DEV__) {
if (partialState === undefined) {
const componentName = getComponentName(Component) || 'Unknown';

if (!didWarnAboutUndefinedDerivedState[componentName]) {
warning(
false,
'%s.getDerivedStateFromProps(): A valid state object (or null) must be returned. ' +
'You may have returned undefined.',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: we're sure in this case, maybe drop the "may"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we used this same wording for the warning from render. I assumed the reason was b'c the user may have return undefined or they may have just forgotten/omitted the return entirely.

Happy to change it though.

componentName,
);
didWarnAboutUndefinedDerivedState[componentName] = true;
}
}
}

if (partialState != null) {
inst.state = Object.assign({}, inst.state, partialState);
}
}
} else {
if (__DEV__) {
if (
Expand Down