Last active
February 9, 2019 14:09
-
-
Save hckhanh/9ea4f1fb1b87dfa2663750e571f2815d to your computer and use it in GitHub Desktop.
higher-order component injected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from "react"; | |
| -import DataSource from "../DataSource"; | |
| +import withSubscription from "./withSubscription"; | |
| const TextBlock = ({ text }) => <>{text}</>; | |
| class BlogPost extends Component { | |
| - constructor(props) { | |
| - super(props); | |
| - this.state = { | |
| - data: DataSource.getBlogPost(props.id) | |
| - }; | |
| - } | |
| - componentDidMount() { | |
| - DataSource.addChangeListener(this.handleChange); | |
| - } | |
| - componentWillUnmount() { | |
| - DataSource.removeChangeListener(this.handleChange); | |
| - } | |
| - handleChange = () => { | |
| - this.setState({ | |
| - data: DataSource.getBlogPost(this.props.id) | |
| - }); | |
| - } | |
| render() { | |
| return <TextBlock text={this.props.data} />; | |
| } | |
| } | |
| +export default withSubscription(BlogPost, (DataSource, props) => | |
| + DataSource.getBlogPost(props.id) | |
| +); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment