Container Components – Learn React with chantastic – Medium

The container component pattern in #ReactJS is crucial:

  • Container ComponentsOne React pattern that’s had the impact on my code is the container component pattern.In Jason Bonta talk High Performance Components, there’s this little gem about container components.The idea is simple:A container does data fetching and then renders its corresponding sub-component.
  • “Corresponding” meaning a component that shares the same name:StockWidgetContainer = StockWidgetTagCloudContainer = = PartyPooperListYou get the idea.Why containers?Say you have a component that displays comments.
  • So, you put everything in one place:class CommentList extends React.Component { this.state = { comments: [] }; componentDidMount() { fetchSomeComments(comments = this.setState({ comments: comments })); } render() { return ( ul {this.state.comments.map(c = ( li{c.body}—{c.author}/li ))} /ul ); }}Your component is responsible for both fetching data and presenting it.
  • There’s nothing “wrong” with this but you miss out on a few benefits of React.ReusabilityCommentList can’t be reused unless under the exact same circumstances.Data structureYour markup components should state expectations of the data they require.
  • This time with a containerFirst, lets pull out data-fetching into a container component.class CommentListContainer extends React.Component { state = { comments: [] }; componentDidMount() { fetchSomeComments(comments = this.setState({ comments: comments })); } render() { return CommentList comments={this.state.comments} /; }}Now, let’s rework CommentList to take comments as a prop.const CommentList = props = ul {props.comments.map(c = ( li{c.body}—{c.author}/li ))} /ulExample CodepenSo, what did we get?We actually got a lot…We’ve separated our data-fetching and rendering concerns.We’ve made our CommentList component reusable.We’ve given CommentList the ability to set PropTypes and fail loudly.I’m a big fan of container components.

Say you have a component that displays comments. You didn’t know about container components. So, you put everything in one place: Your component is responsible for both fetching data and presenting…

@ReactiveConf: The container component pattern in #ReactJS is crucial:

One React pattern that’s had the impact on my code is the container component pattern.

In Jason Bonta talk High Performance Components, there’s this little gem about container components.

The idea is simple:

A container does data fetching and then renders its corresponding sub-component. That’s it.

“Corresponding” meaning a component that shares the same name:

You get the idea.

Why containers?

Say you have a component that displays comments. You didn’t know about container components. So, you put everything in one place:

this.state = { comments: [] };

this.setState({ comments: comments }));

return (

Your component is responsible for both fetching data and presenting it. There’s nothing “wrong” with this but you miss out on a few benefits of React.

CommentList can’t be reused unless under the exact same circumstances.

Your markup components should state expectations of the data they require. PropTypes are great for this.

Our component is opinionated about data structure but has no way of expressing those opinions. This component will break silently if the json endpoint change.

Once again. This time with a container

First, lets pull out data-fetching into a container component.

state = { comments: [] };

this.setState({ comments: comments }));

return ;

Now, let’s rework CommentList to take comments as a prop.

So, what did we get?

We actually got a lot…

We’ve separated our data-fetching and rendering concerns.

We’ve made our CommentList component reusable.

We’ve given CommentList the ability to set PropTypes and fail loudly.

I’m a big fan of container components. They’re stepping up my React game a lot and making my components easier to read. Give them a try and watch Jason’s talk. It’s excellent!

Carry on, nerds.

Container Components – Learn React with chantastic – Medium