Skip to content

Instantly share code, notes, and snippets.

@hckhanh
Last active February 9, 2019 14:10
Show Gist options
  • Save hckhanh/e4a38e7903bb24655b32cd469eec0b54 to your computer and use it in GitHub Desktop.
Save hckhanh/e4a38e7903bb24655b32cd469eec0b54 to your computer and use it in GitHub Desktop.
without higher-order component
import React, { Component } from "react";
import DataSource from "../DataSource";
const TextBlock = ({ text }) => <>{text}</>;
export 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} />;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment