Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import isEqual from 'lodash/fp/isEqual'
  3.  
  4. // let you inject local state to you component
  5.  
  6. const stateful = (mapPropsToInitialState = () => ({})) => Child => {
  7. return class StateFul extends Component {
  8.  
  9. static displayName = `StateFul(${Child.displayName || Child.name})`
  10.  
  11. componentWillMount() {
  12. this.state = mapPropsToInitialState(this.props)
  13. }
  14.  
  15. render() {
  16. return (
  17. <Child
  18. {...this.props}
  19. {...Object.keys(this.state).reduce((acc, key) => ({
  20. ...acc,
  21. [key]: {
  22. value: this.state[key],
  23. onChange: this[`_update${key}`] || (this[`_update${key}`] = v => this.setState({ [key]: v })),
  24. }
  25. }), {})} />
  26. )
  27. }
  28. }
  29. }
  30.  
  31.  
  32. export default stateful
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement