Implementing an article item component

After implementing the article list component, you might decide that it's a good idea to break this component down further, because the item might be rendered in another list on another page. Perhaps the most important aspect of implementing the article list item as its own component is that we don't know how the markup will change in the future.

Another way to look at it is this—if it turns out that we don't actually need the item as its own component, this new component doesn't introduce much indirection or complexity. Without further ado, here's the article item component:

import React, { Component } from 'react';

export default class ArticleItem extends Component {
render() {
// The "article" is mapped from the "ArticleList"
// component. The "onClickToggle()" and
// "onClickRemove()" event handlers are passed
// all the way down from the "MyFeature" component.
const { article, onClickToggle, onClickRemove } = this.props;

return (
<li>
{/* The "onClickToggle()" callback changes
the state of the "MyFeature" component. */}
<a
href={`#{article.id}`}
title="Toggle Summary"
onClick={onClickToggle.bind(null, article.id)}
>
{article.title}
</a>
&nbsp;
{/* The "onClickRemove()" callback changes
the state of the "MyFeature" component. */}
<a
href={`#{article.id}`}
title="Remove"
onClick={onClickRemove.bind(null, article.id)}
>

</a>
<p style={{ display: article.display }}>{article.summary}</p>
</li>
);
}
}

 

Here's the new ArticleItem component being rendered by the ArticleList component:

import React, { Component } from 'react';
import ArticleItem from './ArticleItem';

export default class ArticleList extends Component {
render() {
// The properties include things that are passed in
// from the feature component. This includes the list
// of articles to render, and the two event handlers
// that change state of the feature component. These,
// in turn, are passed to the "ArticleItem" component.
const { articles, onClickToggle, onClickRemove } = this.props;

// Now this component maps to an "<ArticleItem>" collection.
return (
<ul>
{articles.map(i => (
<ArticleItem
key={i.id}
article={i}
onClickToggle={onClickToggle}
onClickRemove={onClickRemove}
/>
))}
</ul>
);
}
}

Do you see how this list just maps the list of articles? What if you wanted to implement another article list that does some filtering too? If so, it's beneficial to have a reusable ArticleItem component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.183.150