Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example component: render the current value of a stream. Support switching streams during the lifecycle of the component
- // N.B. code is untested
- // Old:
- class RenderStream extends Component {
- subscription
- componentWillMount() {
- this.subscribeToStream(this.props.stream)
- }
- componentWillReceiveProps(nextProps) {
- if (this.props.stream !== nextProps.stream) {
- this.subscription.dispose()
- this.subscribeToStream(nextProps.stream)
- }
- }
- render() {
- return <div>Current value: {this.state.value}</div>
- }
- componentWillUnmount() {
- this.subscription.dispose()
- }
- subscribeToStream(stream) {
- // assumption: subscribe triggers immediately
- this.subscription = this.props.stream.subscribe(value => {
- this.setState({ value })
- }
- }
- }
Add Comment
Please, Sign In to add comment