|
| 1 | +--- |
| 2 | +id: container-component |
| 3 | +sidebar_label: Container component (Stateful component) |
| 4 | +title: Container Component (Stateful Component) |
| 5 | +description: Container component (Stateful component) | React Patterns, techniques, tips and tricks in development for Ract developer. |
| 6 | +keywords: ['container component (stateful component)', 'child component', 'reactpatterns', 'react patterns', 'reactjspatterns', 'reactjs patterns', 'react', 'reactjs', 'react techniques', 'react tips and tricks'] |
| 7 | +version: Container component (Stateful component) |
| 8 | +image: /img/reactpatterns-cover.png |
| 9 | +--- |
| 10 | + |
| 11 | +A container does data fetching and then renders its corresponding sub-component. |
| 12 | + |
| 13 | +Assume that you have a component that displays comments, you didn't know about container components, so you put everything in one place. |
| 14 | + |
| 15 | +```jsx |
| 16 | +const fetchSomeComments = cb => |
| 17 | + cb([ |
| 18 | + { author: "Bunlong VAN", body: "Nice to see you here!" }, |
| 19 | + { author: "You", body: "Thanks!" } |
| 20 | + ]) |
| 21 | + |
| 22 | +class CommentList extends React.Component { |
| 23 | + this.state = { comments: [] } |
| 24 | + |
| 25 | + componentDidMount() { |
| 26 | + fetchSomeComments(comments => this.setState({ comments: comments })) |
| 27 | + } |
| 28 | + |
| 29 | + render() { |
| 30 | + return ( |
| 31 | + <ul> |
| 32 | + {this.state.comments.map(c => ( |
| 33 | + <li>{c.body}—{c.author}</li> |
| 34 | + ))} |
| 35 | + </ul> |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +ReactDOM.render( |
| 41 | + <CommentList />, |
| 42 | + document.getElementById("root") |
| 43 | +) |
| 44 | +``` |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +`CommentList` can't be reused unless under the exact same circumstances. |
| 49 | + |
| 50 | +Lets pull out data-fetching into a container component. |
| 51 | + |
| 52 | +```jsx |
| 53 | +class CommentListContainer extends React.Component { |
| 54 | + state = { comments: [] } |
| 55 | + |
| 56 | + componentDidMount() { |
| 57 | + fetchSomeComments(comments => |
| 58 | + this.setState({ comments: comments })) |
| 59 | + } |
| 60 | + |
| 61 | + render() { |
| 62 | + return <CommentList comments={this.state.comments} /> |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +Now, let's rework `CommentList` to take comments as a prop. |
| 68 | + |
| 69 | +```js |
| 70 | +const CommentList = props => |
| 71 | + <ul> |
| 72 | + {props.comments.map(c => ( |
| 73 | + <li>{c.body}—{c.author}</li> |
| 74 | + ))} |
| 75 | + </ul> |
| 76 | +``` |
| 77 | + |
| 78 | +What we got: |
| 79 | + * Separated our data-fetching and rendering concerns |
| 80 | + * Made our `CommentList` component reusable |
| 81 | + * Given `CommentList` the ability to set PropTypes |
0 commit comments