react-connect-context-map
This is a fork of https://github.com/contiamo/react-connect-context.
This module exports a function factory that produces an HOC to wrap a component and provide the value of the context as props to the component, much like the react-redux API. The benefits of this approach versus the traditional Consumer:
- All values are passed as props and can be read in your component's lifecycle methods
- You can trivially export a "pure" version of the component as well as a connected version
- Unlike a Redux container, the connected component can be rendered without being nested in a Provider component
- The factory produces an HOC which can be applied to multiple components
- You can select slices of the context value and map them to props that fit the component API
- You can reference the props passed into the connected component when mapping the context value to props
- You can customize the merging behavior to translate all props fed to the wrapped component
Getting Started
yarn add react-connect-context-map
- At the top of your file,
import { connectContext } from "react-connect-context-map"
- Wrap your component in the function as so:
connectContext(Context.Consumer)(MyComponent)
Examples
Introductory example
// The shape of the context value // The shape of the props passed to your component // Create a new React Context instance // Create an HOC for connecting your component to context // The connected component will receive the props passed in as well as values// from the context object by default // Call the HOC on the component to connect it to context // Render the connected components in the providerrender , document.querySelector"#root"
Mapping context properties to props
This example shows how a context value can be mapped to a different prop name or value.
// The shape of the props that have been derived from context // The shape of the props that are not derived from context and may be// passed to the connected component // The shape of the props passed to the connected component // ContextPropsType can be used when the result of mapContextToProps does// not match the shape of ContextType // OwnPropsType can be used to define the props on the wrapped component// when additional props not inferred from context are provided MergedPropsType, OwnPropsType>Content render , document.querySelector"#root"
Customizing prop merging
This example shows how you can customize the merge behavior. Here we even map a non-context prop to another key.
MergedPropsType, OwnPropsType>Content render , document.querySelector"#root"
mapContextToProps
Using non-object context values with This example shows how you can allow for non-object context values if you provide a custom mapContextToProps
function.
// Context value is not an object ContextType, ContextPropsType> Context.Consumer, // Context value is mapped to an object that conforms to // ContextPropsType MergedPropsType, OwnPropsType>Content render , document.querySelector"#root"
API
connectContext
Factory function that creates a container component HOC to consume context Context
and map values to match ContextProps
.
connectContext // The React Consumer component. ContextConsumer: React.Consumer
createContainer
An HOC that returns a connected component that accepts props OwnProps
and derives ContextProps
to merge into MergedProps
. It returns a component that accepts OwnProps
as props and renders the given component with MergedProps
.
createContainer // The component to connect. Component: React.SFC
MapContextToProps
A function type that maps the consumed context value Context
and props passed to the connected component OwnProps
to a subset of the props that can be derived from context ContextProps
, to pass to the component.
MergeProps
A function type that merges the props that have been mapped from context ContextProps
with the props passed to the connected component OwnProps
to return all the props MergedProps
, to pass to the wrapped component.
Frequently Asked Questions
Can I pick state and only re-render when necessary?
Sure. Consider using PureComponent
or shouldComponentUpdate
to let your components know when or when not to update.
Additionally, unlike Redux, React 16.3 allows the creation of multiple, composable Context
s, so ideally, you'd be using a Context
that is small enough to house just the information that you'd like to reuse in order to properly separate concerns and correctly use the principle of least privilege when passing context around.
Made with ❤️ at Contiamo in Berlin.