react-context-component
Version:
This is a React component that lets you add things in the context. Put simply, the [context feature](https://facebook.github.io/react/docs/context.html) basically lets you to pass some data through all nodes in the components tree.
27 lines (20 loc) • 568 B
JSX
import React from 'react'
import PropTypes from 'prop-types'
const withContext = (contextTypes = {}) => {
const contextTypeKeys = Object.keys(contextTypes)
return (Component) => {
const WithContext = (props, context) => {
const mapContextToProps = contextTypeKeys.reduce(
(acc, key) => {
acc[key] = context[key]
return acc
},
{}
)
return <Component {...props} {...mapContextToProps} />
}
WithContext.contextTypes = contextTypes
return WithContext
}
}
export default withContext