Skip to content

Instantly share code, notes, and snippets.

@hckhanh
Last active February 9, 2019 14:09
Show Gist options
  • Save hckhanh/9ea4f1fb1b87dfa2663750e571f2815d to your computer and use it in GitHub Desktop.
Save hckhanh/9ea4f1fb1b87dfa2663750e571f2815d to your computer and use it in GitHub Desktop.
higher-order component injected
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