Last active
February 9, 2019 14:10
-
-
Save hckhanh/e4a38e7903bb24655b32cd469eec0b54 to your computer and use it in GitHub Desktop.
without higher-order component
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"; | |
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