Skip to content

Instantly share code, notes, and snippets.

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