Recently it has come out that soon React will be deprecating componentWillReceiveProps
and in it's place is the new static function getDerivedStateFromProps
. See more here
I am currently migrating my apps to this new API, but am having an issue withgetDerivedStateFromProps
, due to the fact that I am using the recompose library for higher order components. We make the use of componentWillReceive
props through the library's lifecycle object.
So before moving to the new API, I had this:
export function someHoC () {
return compose(
lifecycle({
componentWillReceiveProps (nextProps) {
const { fetch } = nextProps
if (shouldFetch(this.props, nextProps)) {
fetch()
}
}
})
)
}
This has now changed to the following:
export function someHoC () {
return compose(
lifecycle({
getDerivedStateFromProps (nextProps) {
const { fetch } = nextProps
if (shouldFetch(this.props, nextProps)) {
fetch()
}
}
})
)
}
However, getDerivedStateFromProps
needs to be static, so I am getting the warning regarding that and don't know how to handle it.
warning.js?7f205b4:33 Warning: lifecycle(MyComponent): getDerivedStateFromProps() is defined as an instance method and will be ignored. Instead, declare it as a static method.
What can I do to pass it in as a static lifecycle method into my component?