Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example for loading new external data in response to updated props
// This is an example of how to fetch external data in response to updated props,
// If you are using an async mechanism that does not support cancellation (e.g. a Promise).
class ExampleComponent extends React.Component {
_currentId = null;
state = {
externalData: null
};
static getDerivedStateFromProps(nextProps, prevState) {
// Store prevId in state so we can compare when props change.
// Clear out previously-loaded data (so we don't render stale stuff).
if (nextProps.id !== prevState.prevId) {
return {
externalData: null,
prevId: nextProps.id
};
}
// No state update necessary
return null;
}
componentDidMount() {
this._loadAsyncData(this.props.id);
}
componentDidUpdate(prevProps, prevState) {
if (prevState.externalData === null) {
this._loadAsyncData(this.props.id);
}
}
componentWillUnmount() {
// Prevent potential setState calls after unmount,
// (Since these trigger DEV warnigs)
_currentId = null;
}
render() {
if (this.state.externalData === null) {
// Render loading state ...
} else {
// Render real UI ...
}
}
_loadAsyncData(id) {
if (id === this._currentId) {
// Data for this id is already loading
}
this._currentId = id;
asyncLoadData(id).then(externalData => {
// Only update state if the Promise that has just resolved,
// Reflects the most recently requested external data.
if (id === this._currentId) {
this.setState({ externalData });
}
});
}
}
@MrToph

This comment has been minimized.

Show comment Hide comment
@MrToph

MrToph Mar 28, 2018

I think it's a good idea to link to this React blog post here for all people coming from the latest 16.3 blog post.

There's an alternative there to tracking the mounted state with making the ES6 promise "cancelable".

MrToph commented Mar 28, 2018

I think it's a good idea to link to this React blog post here for all people coming from the latest 16.3 blog post.

There's an alternative there to tracking the mounted state with making the ES6 promise "cancelable".

@ericketts

This comment has been minimized.

Show comment Hide comment
@ericketts

ericketts Mar 28, 2018

is there meant to be a return here?

is there meant to be a return here?

@TimoSta

This comment has been minimized.

Show comment Hide comment
@TimoSta

TimoSta Mar 28, 2018

@bvaughn Out of curiosity, why is getDerivedStateFromProps static?

Wouldn't it be nice to not having to put prevId into the state in this example, and instead checking it like this?

if (nextProps.id !== this.props.prevId) {

TimoSta commented Mar 28, 2018

@bvaughn Out of curiosity, why is getDerivedStateFromProps static?

Wouldn't it be nice to not having to put prevId into the state in this example, and instead checking it like this?

if (nextProps.id !== this.props.prevId) {
@gouegd

This comment has been minimized.

Show comment Hide comment
@gouegd

gouegd Mar 28, 2018

@TimoSta this is explained here, search for "You may wonder why we don’t just pass previous props as a parameter to getDerivedStateFromProps"

gouegd commented Mar 28, 2018

@TimoSta this is explained here, search for "You may wonder why we don’t just pass previous props as a parameter to getDerivedStateFromProps"

@lifeiscontent

This comment has been minimized.

Show comment Hide comment

lifeiscontent commented Mar 29, 2018

@chipit24

This comment has been minimized.

Show comment Hide comment
@chipit24

chipit24 Mar 31, 2018

@gouegd that doesn't answer @TimoSta's question. Do you understand what static methods are? :p

That piece from the blog post provides some justification as to why prevProps isn't passed as a parameter to the static method, but it doesn't explain why they decided to make the lifecycle method static.

Doing some Googling, I came across the RFC for static lifecycle methods and the following justification was provided:

Making certain lifecycles static to prevent unsafe access of instance properties.

Like the blog post mentions, by "unsafe" they probably mean "more likely to have bugs in future versions of React, especially once async rendering is enabled".

@gouegd that doesn't answer @TimoSta's question. Do you understand what static methods are? :p

That piece from the blog post provides some justification as to why prevProps isn't passed as a parameter to the static method, but it doesn't explain why they decided to make the lifecycle method static.

Doing some Googling, I came across the RFC for static lifecycle methods and the following justification was provided:

Making certain lifecycles static to prevent unsafe access of instance properties.

Like the blog post mentions, by "unsafe" they probably mean "more likely to have bugs in future versions of React, especially once async rendering is enabled".

@inomdzhon

This comment has been minimized.

Show comment Hide comment
@inomdzhon

inomdzhon Apr 1, 2018

Sure, @lifeiscontent is right, we missed this here

inomdzhon commented Apr 1, 2018

Sure, @lifeiscontent is right, we missed this here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment