Personal Dashboard

Control.js 700B

123456789101112131415161718192021
  1. import React from 'react'
  2. import { mapValues } from 'lodash'
  3. export default (initialState, handlers) => Component =>
  4. class extends React.Component {
  5. state = initialState || {}
  6. stateHandler = { setState: this.setState.bind(this) }
  7. getProps = () => ({ ...this.props, ...this.handlersToProps })
  8. prepareHandler = h => typeof h !== 'function' ? h : (...args) => h({
  9. ...this.stateHandler, state: this.state, props: this.getProps()
  10. }, ...args)
  11. handlersToProps = !handlers ? null : _.mapValues(handlers, this.prepareHandler)
  12. render() {
  13. return Component(this.getProps(), this.state, this.stateHandler)
  14. }
  15. }