Last active
May 4, 2018 06:47
-
-
Save andycarrell/11796c56b04f28700b69d974f39cfea8 to your computer and use it in GitHub Desktop.
This file contains 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
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, | |
}; | |
}; |
This file contains 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
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, | |
}; | |
}; |
This file contains 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
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