Skip to content

Commit 64fee67

Browse files
Benjamin VanEverytimdorr
Benjamin VanEvery
authored andcommitted
Clarify details on connecting with React (#2527)
* Clarify details on connecting with React 1. `VisibleTodoList` has not yet been introduced in the documentation 2. It's slightly confusing to see the combination of a component and container without preamble in a "here's the rest of the code" section. 3. What is a `ref`? * The App can't be declared until after containers This breaks the organization of components and containers, but clarifies the interrelationship between the two. As well as dispels confusion about whence those two yet-to-be-mentioned containers came.
1 parent 887bf07 commit 64fee67

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

docs/basics/UsageWithReact.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,6 @@ const Footer = () => (
226226
export default Footer
227227
```
228228

229-
#### `components/App.js`
230-
231-
```js
232-
import React from 'react'
233-
import Footer from './Footer'
234-
import AddTodo from '../containers/AddTodo'
235-
import VisibleTodoList from '../containers/VisibleTodoList'
236-
237-
const App = () => (
238-
<div>
239-
<AddTodo />
240-
<VisibleTodoList />
241-
<Footer />
242-
</div>
243-
)
244-
245-
export default App
246-
```
247-
248229
### Implementing Container Components
249230

250231
Now it's time to hook up those presentational components to Redux by creating some containers. Technically, a container component is just a React component that uses [`store.subscribe()`](../api/Store.md#subscribe) to read a part of the Redux state tree and supply props to a presentational component it renders. You could write a container component by hand, but we suggest instead generating container components with the React Redux library's [`connect()`](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) function, which provides many useful optimizations to prevent unnecessary re-renders. (One result of this is that you shouldn't have to worry about the [React performance suggestion](https://facebook.github.io/react/docs/advanced-performance.html) of implementing `shouldComponentUpdate` yourself.)
@@ -372,6 +353,8 @@ export default VisibleTodoList
372353

373354
#### `containers/AddTodo.js`
374355

356+
Recall as [mentioned previously](#designing-other-components), both the presentation and logic for the `AddTodo` component are mixed into a single definition.
357+
375358
```js
376359
import React from 'react'
377360
import { connect } from 'react-redux'
@@ -409,6 +392,28 @@ AddTodo = connect()(AddTodo)
409392
export default AddTodo
410393
```
411394

395+
If you are unfamiliar with the `ref` attribute, please read this [documentation](https://facebook.github.io/react/docs/refs-and-the-dom.html) to familiarize yourself with the recommended use of this attribute.
396+
397+
### Tying the containers together within a component
398+
#### `components/App.js`
399+
400+
```js
401+
import React from 'react'
402+
import Footer from './Footer'
403+
import AddTodo from '../containers/AddTodo'
404+
import VisibleTodoList from '../containers/VisibleTodoList'
405+
406+
const App = () => (
407+
<div>
408+
<AddTodo />
409+
<TodoList />
410+
<Footer />
411+
</div>
412+
)
413+
414+
export default App
415+
```
416+
412417
## Passing the Store
413418

414419
All container components need access to the Redux store so they can subscribe to it. One option would be to pass it as a prop to every container component. However it gets tedious, as you have to wire `store` even through presentational components just because they happen to render a container deep in the component tree.

0 commit comments

Comments
 (0)