Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Adds React.createContext API support #1548

Closed
wants to merge 14 commits into from

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Mar 8, 2018

Release notes: none

Adds React.createContext support to the React compiler. Updates Yarn dependencies to use React 16.3 pre-release version so tests can validate new API works with the compiler.

@trueadm trueadm changed the title Add React.createContext support Adds React.createContext support Mar 8, 2018
@trueadm trueadm changed the title Adds React.createContext support Adds React.createContext API support Mar 8, 2018
@sebmarkbage
Copy link
Contributor

We need to handle the cases of sibling consumers without a new provider:

<Provider value="a">
   <Provider value="b">
     <Consumer />
   </Provider>
   <Consumer />
</Provider>
<div>
   <Provider value="b">
     <Consumer />
   </Provider>
   <Consumer />
</div>

// the JSX transform converts to React, so we need to add it back in
this['React'] = React;
var { Provider, Consumer } = React.createContext(null);
// this is done otherwise the test fails
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a very helpful comment. :) Why does it fail if you don't?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we don't the test that runs eval on the getTrials function doesn't correctly pick up the closed over value. This has been a common pain with how we run tests this way – nothing else we can do for now.

@@ -163,7 +163,8 @@ export type ReactEvaluatedNode = {
| "UNKNOWN_TYPE"
| "RENDER_PROPS"
| "UNSUPPORTED_COMPLETION"
| "ABRUPT_COMPLETION",
| "ABRUPT_COMPLETION"
| "NORMAL",
Copy link
Contributor

Choose a reason for hiding this comment

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

What does normal mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Normal is just a default case where neither folding or inlining happens – typically for things like React.Fragment or Context.Provider and Context.Consumer.

// get the "render" prop child off the instance
let renderProp = Get(this.realm, propsValue, "children");
if (renderProp instanceof ECMAScriptSourceFunctionValue && renderProp.$Call) {
if (this.componentTreeConfig.firstRenderOnly) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this for first render only? Can't we always fold context if we know its provider?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because on update render, we need keep the children closure otherwise updates to it won't work.

}

newBranchState.addContext(contextConsumer);
if (this.componentTreeConfig.firstRenderOnly) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this first render only? If we know the value (which might be abstract), it should be fine for updates too. Since it's abstract it'll be able to update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to be on the safe side right now for updates, as we don't know if a parent in an unknown parent tree exists.

Copy link
Contributor

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

Seems ok, but I don't understand why this is first render only instead of for updates too. Should be ok to do for updates too.

At some point we should start implementing more of this reconciliation logic in JS evaluated inside of Prepack instead. With some helpers exposed to it.

That way we can run tests against it in the React repo and run tests against it there. This is reaching into some implementation details that are quite likely to change.

@sebmarkbage
Copy link
Contributor

sebmarkbage commented Mar 9, 2018

I think this doesn't preserved the "changed bits" feature. Which is ok for now.

@trueadm
Copy link
Contributor Author

trueadm commented Mar 9, 2018

@sebmarkbage How does changed bits work? I don't mind adding it in a follow up PR. Thanks for the review. I plan on following up on the extra stuff on the plane tomorrow.

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@trueadm is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@trueadm is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@trueadm is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@trueadm is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants