Expanded DEV-only warnings for gDSFP and legacy lifecycles#12419
Expanded DEV-only warnings for gDSFP and legacy lifecycles#12419bvaughn merged 7 commits intofacebook:masterfrom
Conversation
|
@bvaughn will this also apply to other new lifecycle methods, like |
|
I have no current plans for that to be the case, no. |
|
I've updated the PR description to more clearly reflect the considerations that went into this decision. Please let me know if there are any concerns about vague wording or if I omitted anything. |
| }).toWarnDev('Defines both componentWillReceiveProps'); | ||
| }).toWarnDev( | ||
| 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API.\n\n' + | ||
| 'Unknown uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' + |
There was a problem hiding this comment.
Using "Unknown" as a proper noun feels kind of confusing, but I can't think of something better.
There was a problem hiding this comment.
Agreed. I wonder if there's a way I could improve this by reformatting the message slightly.
There was a problem hiding this comment.
Maybe "An unknown component uses..."?
There was a problem hiding this comment.
That makes it seem like React doesn't know which component, when really- it just doesn't know what the name fo the component is.
Maybe "An unnamed component" ?
There was a problem hiding this comment.
Actually I think just plain "Component" might be okay, and more inline with other places.
There was a problem hiding this comment.
In what cases does React not know the name of a component? "An unnamed component" sounds better than "Unknown" to me 👍
There was a problem hiding this comment.
create-react-class case
The biggest reason is that the react-lifecycles-compat polyfill relies on unsafe legacy lifecycles A second argument is that, conceptually, it doesn't make sense to combine the new and old portions of the component API (outside of the case of the polyfill). The new API exists as a safe alternative to the old, not as a compliment. |
|
@bvaughn to clarify, I'm asking for clarification on your response to my question:
I understand the argument for prevent users from mixing new and old APIs, but I'm curious why this won't apply to other new lifecycle methods, like |
|
Let's move this conversation over to the As I think more about how I would implement |
|
FYI @aweary, I added a polyfill section to the other RFC. |
|
I have the same problem. Can When I remove Examples are as follows: import React, { Component } from 'react'
export default class LifeCycle extends Component {
constructor(props) {
super(props)
this.state = {}
}
static getDerivedStateFromProps() {
return null
}
UNSAFE_componentWillMount() {}
componentDidMount() {}
render() {
return (
<div>React Life Cycle</div>
)
}
} |
No. That's what the warning says. |
After a lot of discussion and careful consideration, the team has reaffirmed the initial decision to not call unsafe legacy lifecycles
componentWillMount,componentWillReceiveProps, andcomponentWillUpdatefor any component that defines the new staticgetDerivedStateFromPropslifecycle. (This applies to the "UNSAFE_" aliases as well.)The reason for this decision ultimately boils down to support for the react-lifecycles-compat polyfill and concerns about consistent, predictable behavior for future version of React.
As @jquense pointed out with issue #12411, conditional lifecycle behavior is a new thing, and it is not intuitive. For this reason, we needed to improve the granularity and wording of the DEV warning. This PR does that, first by expanding the warning conditionals to explicitly check for
componentWillMountandcomponentWillUpdateas well (rather than justcomponentWillReceiveProps) and secondly by explicitly stating that "Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API".The entire new DEV warning is:
Tests have been updated as well to verify the new behavior.
Resolves #12411