Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Created March 3, 2018 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrhodes/83199987094248924e5cc2633b6faa52 to your computer and use it in GitHub Desktop.
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: 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