|
| 1 | +<html> |
| 2 | +<head> |
| 3 | + <title>Container component | React Patterns & Techniques</title> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> |
| 6 | + <meta name="description" content="Container component. You should avoid async initialization in componentWillMount()"> |
| 7 | + <meta name="keywords" content="react-patterns, patterns, react, javascript, react-native, reactjs, component, components, front-end, frontend, front-end-development, frontend-web, frontend-webdevelopment, frontend-components, frontend-app, react-app, react-component, react-components, react-techniques, reactjs-patterns"> |
| 8 | + <meta name=" Code Facebook" content=" [email protected]" > |
| 9 | + <link href="css/styles.css" rel="stylesheet"> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + <div id="container"> |
| 13 | + <header> |
| 14 | + <h1> |
| 15 | + <a href="/">React Patterns & Techniques</a> |
| 16 | + </h1> |
| 17 | + </header> |
| 18 | + <h3>Table of Contents</h3> |
| 19 | + <ul> |
| 20 | + <ul> |
| 21 | + <li><a href="async-initialization-in-componentdidmount.html">Async initialization in componentDidMount()</a></li> |
| 22 | + <li><a href="container-component.html">Container component</a></li> |
| 23 | + </ul> |
| 24 | + </ul> |
| 25 | + <h3>Container component</h3> |
| 26 | + <p> |
| 27 | + A container does data fetching and then renders its corresponding sub-component. |
| 28 | + </p> |
| 29 | + <p> |
| 30 | + Assume that you have a component that displays comments. You didn’t know about container components. So, you put everything in one place. |
| 31 | + </p> |
| 32 | + <pre class="screen"> |
| 33 | +const fetchSomeComments = cb => |
| 34 | + cb([ |
| 35 | + { author: "Bunlong VAN", body: "Nice to see you here!" }, |
| 36 | + { author: "You", body: "Thanks!" } |
| 37 | + ]); |
| 38 | + |
| 39 | +class CommentList extends React.Component { |
| 40 | + this.state = { comments: [] }; |
| 41 | + |
| 42 | + componentDidMount() { |
| 43 | + fetchSomeComments(comments => this.setState({ comments: comments })); |
| 44 | + } |
| 45 | + |
| 46 | + render() { |
| 47 | + return ( |
| 48 | + <ul> |
| 49 | + {this.state.comments.map(c => ( |
| 50 | + <li>{c.body}—{c.author}</> |
| 51 | + ))} |
| 52 | + </ul> |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +ReactDOM.render( |
| 58 | + <CommentList />, |
| 59 | + document.getElementById("root") |
| 60 | +); |
| 61 | + </pre> |
| 62 | + <p> |
| 63 | + 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. |
| 64 | + </p> |
| 65 | + <p> |
| 66 | + CommentList can’t be reused unless under the exact same circumstances. |
| 67 | + </p> |
| 68 | + <p> |
| 69 | + Lets pull out data-fetching into a container component. |
| 70 | + </p> |
| 71 | + <pre class="screen"> |
| 72 | +class CommentListContainer extends React.Component { |
| 73 | + state = { comments: [] }; |
| 74 | + |
| 75 | + componentDidMount() { |
| 76 | + fetchSomeComments(comments => |
| 77 | + this.setState({ comments: comments })); |
| 78 | + } |
| 79 | + |
| 80 | + render() { |
| 81 | + return <CommentList comments={this.state.comments} />; |
| 82 | + } |
| 83 | +} |
| 84 | + </pre> |
| 85 | + <p> |
| 86 | + Now, let’s rework CommentList to take comments as a prop. |
| 87 | + </p> |
| 88 | + <pre class="screen"> |
| 89 | +const CommentList = props => |
| 90 | + <ul> |
| 91 | + {props.comments.map(c => ( |
| 92 | + <li>{c.body}—{c.author}</li> |
| 93 | + ))} |
| 94 | + </ul> |
| 95 | + </pre> |
| 96 | + <p class="caption"> |
| 97 | + What we got: |
| 98 | + </p> |
| 99 | + <ul class="caption"> |
| 100 | + <li> |
| 101 | + Separated our data-fetching and rendering concerns. |
| 102 | + </li> |
| 103 | + <li> |
| 104 | + Made our CommentList component reusable. |
| 105 | + </li> |
| 106 | + <li> |
| 107 | + Given CommentList the ability to set PropTypes. |
| 108 | + </li> |
| 109 | + </ul> |
| 110 | + </div> |
| 111 | +</body> |
| 112 | +</html> |
0 commit comments