-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expanded DEV-only warnings for gDSFP and legacy lifecycles #12419
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bvaughn thanks for clarifying! Can you expand on why this same behavior (conditional lifecycles) won't extend to other new lifecycle methods? What is special about gDSFP?
}).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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I wonder if there's a way I could improve this by reformatting the message slightly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "An unknown component uses..."?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think just plain "Component" might be okay, and more inline with other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
, andcomponentWillUpdate
for any component that defines the new staticgetDerivedStateFromProps
lifecycle. (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
componentWillMount
andcomponentWillUpdate
as 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