Guest User

Untitled

a guest
Jul 18th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. // Example component: render the current value of a stream. Support switching streams during the lifecycle of the component
  2. // N.B. code is untested
  3.  
  4. // Old:
  5. class RenderStream extends Component {
  6. subscription
  7.  
  8. componentWillMount() {
  9. this.subscribeToStream(this.props.stream)
  10. }
  11.  
  12. componentWillReceiveProps(nextProps) {
  13. if (this.props.stream !== nextProps.stream) {
  14. this.subscription.dispose()
  15. this.subscribeToStream(nextProps.stream)
  16. }
  17. }
  18.  
  19. render() {
  20. return <div>Current value: {this.state.value}</div>
  21. }
  22.  
  23. componentWillUnmount() {
  24. this.subscription.dispose()
  25. }
  26.  
  27. subscribeToStream(stream) {
  28. // assumption: subscribe triggers immediately
  29. this.subscription = this.props.stream.subscribe(value => {
  30. this.setState({ value })
  31. }
  32. }
  33. }
Add Comment
Please, Sign In to add comment