Last active
February 9, 2019 14:10
-
-
Save hckhanh/3b5a1331c172398d5e7a51fabce74114 to your computer and use it in GitHub Desktop.
without higher-order component
Raw
CommentList.js
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
Show hidden characters
import React, { Component } from "react"; | |
import DataSource from "../DataSource"; | |
const Comment = ({ comment }) => <>{comment}</>; | |
export class CommentList extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
data: DataSource.getComments() | |
}; | |
} | |
componentDidMount() { | |
DataSource.addChangeListener(this.handleChange); | |
} | |
componentWillUnmount() { | |
DataSource.removeChangeListener(this.handleChange); | |
} | |
handleChange = () => { | |
this.setState({ | |
data: DataSource.getComments() | |
}); | |
}; | |
render() { | |
return ( | |
<> | |
{this.props.data.map(comment => ( | |
<Comment comment={comment} key={comment.id} /> | |
))} | |
</> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment