Here's what the article list component implementation looks like:
import React, { Component } from 'react';
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.
const { articles, onClickToggle, onClickRemove } = this.props;
return (
<ul>
{articles.map(article => (
<li key={article.id}>
{/* 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>
{/* 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>
))}
</ul>
);
}
}
You're just taking the relevant JSX out of the monolithic component and putting it here. Now let's see what the feature component JSX looks like:
render() {
const { articles, title, summary } = this.data.toJS();
return (
<section>
<header>
<h1>Articles</h1>
<input
placeholder="Title"
value={title}
onChange={this.onChangeTitle}
/>
<input
placeholder="Summary"
value={summary}
onChange={this.onChangeSummary}
/>
<button onClick={this.onClickAdd}>Add</button>
</header>
{/* Now the list of articles is rendered by the
"ArticleList" component. This component can
now be used in several other components. */}
<ArticleList
articles={articles}
onClickToggle={this.onClickToggle}
onClickRemove={this.onClickRemove}
/>
</section>
);
}
The list of articles is now rendered by the <ArticleList> component. The list of articles to render is passed to this component as a property as well as two of the event handlers.
Wait, why are we passing event handlers to a child component? The reason is so that the ArticleList component doesn't have to worry about state or how the state changes. All it cares about is rendering content, and making sure the appropriate event callbacks are hooked up to the appropriate DOM elements. This is a container component concept that I'll expand upon later in this chapter.