Refactoring class components using hooks
Prior to the addition of hooks to React, we would often end up using class-based components just because the component had state data to maintain. Hooks exist so that you can implement React components using regular functions and still have access to the React APIs that you used to access through class attributes and methods. In this section, we'll rewrite the MyFeature
component so that it's a function and it uses the useState()
hook.
First, let's take a look at the functional version of MyFeature
:
function MyFeature({ addArticle, articleList }) { const [articles, setArticles] = React.useState([ { id: id.next(), title: "Article 1", summary: "Article 1 Summary", display: "none", }, ...