Skip to content

Instantly share code, notes, and snippets.

@manoelneto
Created October 6, 2018 14:09
Show Gist options
  • Save manoelneto/5b3d1d8452516c4fe11aa378ee46ce74 to your computer and use it in GitHub Desktop.
Save manoelneto/5b3d1d8452516c4fe11aa378ee46ce74 to your computer and use it in GitHub Desktop.
import memoize from "memoize-one"
const PostList = ({posts}) => (
posts.map(post => <Post post={post} />)
)
class PostPage extends Component {
state = {
posts: [],
category: ""
}
componentDidMount() {
fetchPost().then(posts => this.setState({posts}))
}
filterPosts = memoize(
(posts, category) => posts.filter(post => post.category === category)
)
render() {
const {
category
} = this.state;
const posts = this.filterPosts(this.state.posts, category)
return <>
<input
type="text"
onChange={e => this.setState({category: e.target.value})}
value={category}
/>
<PostList posts={this.state.posts} />
</>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment