Last active
February 9, 2019 14:08
-
-
Save hckhanh/748b3b4fbbee032f5c035621aa4302b1 to your computer and use it in GitHub Desktop.
Full stateful implementation
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