Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. //...other imports...
  2. import React, {Component} from 'react'
  3. import MyUserComponent from './MyUserComponent'
  4. import appState from './appState'
  5.  
  6. // setup initial app state as object
  7. const initAppState = {
  8. loading: false,
  9. userObject: {},
  10. moreStuff: []
  11. }
  12.  
  13. // call appState with initial state object to get state manager
  14. const stateManager = appState(initAppState)
  15.  
  16. class AppContainer extends Component {
  17. constructor(props) {
  18. super(props)
  19. // set the stateManager's emitter callback so container state gets updates
  20. stateManager.emitter.callback = x => this.setState(x)
  21. // load initial container state from the stateManager
  22. this.state = stateManager.state
  23. }
  24.  
  25. render() {
  26. const {emitter} = stateManager
  27. const {userObject} = this.state
  28. // pass the emitter down to any component that needs
  29. // to communicate a state change request via
  30. // props.emitter.emit('update', {...newState})
  31. return (
  32. <div>
  33. <MyUserComponent {emitter} {userObject}/>
  34. </div>
  35. )
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement