Created
March 3, 2018 22:49
-
-
Save jasonrhodes/83199987094248924e5cc2633b6faa52 to your computer and use it in GitHub Desktop.
Trying to understand the new React 16.3 createContext source code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file: https://github.com/facebook/react/blob/fb85cf2e9c52a4b2999144a89a6ee8256aec55c7/packages/react/src/ReactContext.js | |
* if you remove the <flow> stuff and the "calculate changed bits" stuff, it appears to be | |
* a function that, when called, returns an object literal with some weird circular references? | |
* | |
* How is this working / what am I missing here? | |
*/ | |
export function createContext(defaultValue) { | |
const context = { | |
$$typeof: REACT_CONTEXT_TYPE, | |
defaultValue, | |
currentValue: defaultValue, | |
// These are circular | |
Provider: (null: any), | |
Consumer: (null: any), | |
}; | |
context.Provider = { | |
$$typeof: REACT_PROVIDER_TYPE, | |
context, | |
}; | |
context.Consumer = context; | |
return context; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment