UNPKG

contiago-toolbar

Version:

One of the options for outputting content from contiago xml-server

38 lines (32 loc) 910 B
import React from 'react'; import PropTypes from 'prop-types'; import { push } from './actions'; class WithStateRouter extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function render() { const { router } = this.context; const childrenWithProps = React.Children.map(this.props.children, (child) => React.cloneElement(child, { router }) ); return <div>{childrenWithProps}</div>; } } WithStateRouter.propTypes = { children: PropTypes.oneOfType([ PropTypes.element.isRequired, PropTypes.func.isRequired, ]), }; WithStateRouter.contextTypes = { router: PropTypes.object.isRequired, }; export const withRouter = (Component) => () => ( <WithStateRouter> <Component /> </WithStateRouter> ); export const createRouter = (store) => { const router = {}; router.push = (newRoute) => store.dispatch(push(newRoute)); return router; };