Skip to content

Instantly share code, notes, and snippets.

@andycarrell
Last active May 4, 2018 06:47
Show Gist options
  • Save andycarrell/11796c56b04f28700b69d974f39cfea8 to your computer and use it in GitHub Desktop.
Save andycarrell/11796c56b04f28700b69d974f39cfea8 to your computer and use it in GitHub Desktop.
import React, { createContext as reactCreateContext } from 'react';
export const createContext = value => {
const context = reactCreateContext(value);
// this is actually a component, which maps a 'render' named prop.
// should have a better name etc.
const consumer = ({ render }) => <context.Consumer>{render}</context.Consumer>;
return {
Provider: context.Provider,
Consumer: consumer,
};
};
export const createContext = value => {
const context = reactCreateContext(value);
export const withContext = Component => {
const C = props => (
<context.Consumer>
{context => <Component {...context} {...props} />}
</context.Consumer>
);
C.displayName = `withContext(${Component.displayName || Component.name})`;
return C;
};
return {
Provider: context.Provider,
Consumer: context.Consumer,
withContext,
};
};
import React, { createContext as reactCreateContext } from 'react';
export const createContext = value => {
const { Provider, Consumer } = reactCreateContext(value);
// maps a prop named 'render' to function as a child
const RenderPropConsumer = ({ render }) => <Consumer>{render}</Consumer>;
const withContext = Component => {
const C = props => (
<Consumer>
{context => <Component {...context} {...props} />}
</Consumer>
);
C.displayName = `withContext(${Component.displayName || Component.name})`;
return C;
}
return {
Provider,
Consumer,
RenderPropConsumer,
withContext,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment