Skip to content
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

Invoke both legacy and UNSAFE_ lifecycles when both are present #12134

Merged
merged 2 commits into from
Feb 1, 2018

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Feb 1, 2018

This is to support edge cases, specifically with create-react-class, where a mixin defines a legacy lifecycle but the component being created defines an UNSAFE_ one (or vice versa).

I did not warn about this case because the warning would be a bit redundant with the deprecation warning which we will soon be enabling. I could be convinced to change my stance here though.

Added some new tests for this on the various renders that it impacts (default, ssr, shallow). Also verified that this change fixes several (all that I've tested so far) of the test that failed internally for me on www after running the rename codemod.

This is to support edge cases with eg create-react-class where a mixin defines a legacy lifecycle but the component being created defines an UNSAFE one (or vice versa).

I did not warn about this case because the warning would be a bit redundant with the deprecation warning which we will soon be enabling. I could be convinced to change my stance here though.
@gaearon
Copy link
Collaborator

gaearon commented Feb 1, 2018

I think it's confusing that in some cases mixing "old" and "new" works (UNSAFE_cWRP + cWRP) but in other cases it doesn't (gDSFP + cWRP).

Did you consider always enforcing just one case? e.g. you either have cWRP, or UNSAFE_cWRP, or gDSFP, but never a mix, otherwise throw? Are there downsides to this?

@bvaughn
Copy link
Contributor Author

bvaughn commented Feb 1, 2018

I think it's confusing that in some cases mixing "old" and "new" works (UNSAFE_cWRP + cWRP) but in other cases it doesn't (gDSFP + cWRP).

Yeah, I see that POV.

I think it's slightly different though, since the presence of gDSFP indicates that a human took the time to migrate a component and (presumably) to think through the implications. We also warn about this case.

The presence of U_cWRP just means that someone ran a codemod script and didn't necessarily think through anything further. And if they may have external dependencies with legacy mixins, as I've seen internally.

Did you consider always enforcing just one case? e.g. you either have cWRP, or UNSAFE_cWRP, or gDSFP, but never a mix? Are there downsides to this?

It would break the polyfill, for one. Our approach to backwards compat relies on the presence of gDSFP and cWRP.

We could enforce that cWRP and U_cWRP can't co-exist but I think that might just make the transition more pain (actual example: me, running codemods internally).

@gaearon
Copy link
Collaborator

gaearon commented Feb 1, 2018

It would break the polyfill, for one. Our approach to backwards compat relies on the presence of gDSFP and cWRP.

I don't see why.

Old React versions don't have the check / support for new lifecycles.

I imagined that polyfill on the newer versions would not "declare" fake cWRP/cWM in the first place. But maybe that changed since my initial polyfill proposal. In that case it makes sense.

@bvaughn
Copy link
Contributor Author

bvaughn commented Feb 1, 2018

It would break the polyfill, for one. Our approach to backwards compat relies on the presence of gDSFP and cWRP.

I don't see why.

I imagined that polyfill on the newer versions would not "declare" fake cWRP/cWM in the first place. But maybe that changed since my initial polyfill proposal. In that case it makes sense.

The polyfill always defines the legacy methods, because feature detection ended up feeling too brittle (particularly when considering React-like libraries like Preact).

Instead, I changed React not to warn about cWRP+gDSFP if it detected that the polyfill had added cWRP and to not invoke any of the unsafe lifecycles if gDSFP was present.

This relates to PR #12105

@gaearon
Copy link
Collaborator

gaearon commented Feb 1, 2018

These conditions are getting nasty. I'm nervous about creating a bug there in some permutation, now or later, but I trust you various permutations are tested. Can't wait for 17.

} else if (typeof Component.getDerivedStateFromProps !== 'function') {
}
if (
inst.UNSAFE_componentWillMount &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe change this to test for function type explicitly for consistency? I know it didn't before but still.

@bvaughn bvaughn merged commit 4eed18d into facebook:master Feb 1, 2018
@bvaughn bvaughn deleted the crc-mixin-lifecycles-fix branch February 1, 2018 19:16
@boondog3738
Copy link

What is this all about

@gaearon
Copy link
Collaborator

gaearon commented Feb 1, 2018

NMinhNguyen referenced this pull request in enzymejs/react-shallow-renderer Jan 29, 2020
* Invoke both legacy and UNSAFE_ lifecycles when both are present

This is to support edge cases with eg create-react-class where a mixin defines a legacy lifecycle but the component being created defines an UNSAFE one (or vice versa).

I did not warn about this case because the warning would be a bit redundant with the deprecation warning which we will soon be enabling. I could be convinced to change my stance here though.

* Added explicit function-type check to SS ReactPartialRenderer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants