|
1 | 1 | ---
|
2 |
| -id: getting-started |
3 |
| -title: Getting Started |
| 2 | +id: basic-tutorial |
| 3 | +title: Basic Tutorial |
4 | 4 | hide_title: true
|
5 |
| -sidebar_label: Getting Started |
| 5 | +sidebar_label: Basic Tutorial |
6 | 6 | ---
|
7 | 7 |
|
8 |
| -# Getting Started |
| 8 | +# Basic Tutorial |
9 | 9 |
|
10 |
| -[React-Redux](https://github.com/reduxjs/react-redux) is the official [React](https://reactjs.org/) binding for [Redux](https://redux.js.org/). It lets your React components read data from a Redux store, and dispatch actions to the store to update data. |
11 |
| - |
12 |
| -## Installation |
13 |
| - |
14 |
| -To use React-Redux with your React app: |
15 |
| - |
16 |
| -```bash |
17 |
| -npm install --save react-redux |
18 |
| -``` |
19 |
| - |
20 |
| -or |
21 |
| - |
22 |
| -```bash |
23 |
| -yarn add react-redux |
24 |
| -``` |
25 |
| - |
26 |
| -<!-- perhaps add link to an extra quick start section? --> |
27 |
| - |
28 |
| -## `Provider` and `connect` |
29 |
| - |
30 |
| -React-Redux consists of two main pieces. The first is a component called `<Provider />`, which makes the Redux store available to the rest of your app: |
31 |
| - |
32 |
| -```js |
33 |
| -import React from "react"; |
34 |
| -import ReactDOM from "react-dom"; |
35 |
| - |
36 |
| -import { Provider } from "react-redux"; |
37 |
| -import store from "./store"; |
38 |
| - |
39 |
| -import App from "./App"; |
40 |
| - |
41 |
| -const rootElement = document.getElementById("root"); |
42 |
| -ReactDOM.render( |
43 |
| - <Provider store={store}> |
44 |
| - <App /> |
45 |
| - </Provider>, |
46 |
| - rootElement |
47 |
| -); |
48 |
| -``` |
49 |
| - |
50 |
| -The second piece is a function called `connect()`, which encapsulates the process of talking to the store. |
51 |
| - |
52 |
| -It enables you to: |
53 |
| - |
54 |
| -- Read data from the Redux `store` into your app’s connected components as props |
55 |
| -- Dispatch actions to your `store` from any of your app’s connected components |
56 |
| - |
57 |
| -Correspondingly, the `connect` function takes two arguments, both optional: |
58 |
| - |
59 |
| -- `mapStateToProps`: called every time the store state changes. It receives the entire store state, and should return an object of data this component needs. |
60 |
| - |
61 |
| -- `mapDispatchToProps`: this parameter can either be a function, or an object. |
62 |
| - - If it’s a function, it will be called once on component creation. It will receive `dispatch` as an argument, and should return an object full of functions that use `dispatch` to dispatch actions. |
63 |
| - - If it’s an object full of action creators, each action creator will be turned into a prop function that automatically dispatches its action when called. **Note**: We recommend using this “object shorthand” form. |
64 |
| - |
65 |
| -Normally, you’ll call `connect` in this way: |
66 |
| - |
67 |
| -```js |
68 |
| -const mapStateToProps = (state, ownProps) => ({ |
69 |
| - // ... computed data from state and optionally ownProps |
70 |
| -}); |
71 |
| - |
72 |
| -const mapDispatchToProps = { |
73 |
| - // ... normally is an object full of action creators |
74 |
| -}; |
75 |
| - |
76 |
| -// `connect` returns a new function that accepts the component to wrap: |
77 |
| -const connectToStore = connect( |
78 |
| - mapStateToProps, |
79 |
| - mapDispatchToProps |
80 |
| -); |
81 |
| -// and that function returns the connected, wrapper component: |
82 |
| -const ConnectedComponent = connectToStore(Component); |
83 |
| - |
84 |
| -// We normally do both in one step, like this: |
85 |
| -connect( |
86 |
| - mapStateToProps, |
87 |
| - mapDispatchToProps |
88 |
| -)(Component); |
89 |
| -``` |
| 10 | +To see how to use React-Redux in practice, we’ll show a step-by-step example by creating a todo list app. |
90 | 11 |
|
91 | 12 | ## A Todo List Example
|
92 | 13 |
|
93 |
| -To see this in practice, we’ll show a step-by-step example by creating a todo list app using React-Redux. |
94 |
| - |
95 | 14 | **Jump to**
|
96 | 15 |
|
97 | 16 | - 🤞 [Just show me the code](https://codesandbox.io/s/9on71rvnyo)
|
98 | 17 | - 👆 [Providing the store](#providing-the-store)
|
99 |
| -- ✌️ [Common Ways of Calling Connect](#common-ways-of-calling-connect) |
| 18 | +- ✌️ [Connecting the Component](#connecting-the-components) |
100 | 19 |
|
101 | 20 | **The React UI Components**
|
102 | 21 |
|
@@ -177,9 +96,41 @@ Notice how our `<TodoApp />` is now wrapped with the `<Provider />` with `store`
|
177 | 96 |
|
178 | 97 | ### Connecting the Components
|
179 | 98 |
|
180 |
| -Our components need to read values from the Redux store (and re-read the values when the store updates). They also need to dispatch actions to trigger updates. |
| 99 | +React-Redux provides a `connect` function for you to read values from the Redux store (and re-read the values when the store updates). |
| 100 | + |
| 101 | +The `connect` function takes two arguments, both optional: |
| 102 | + |
| 103 | +- `mapStateToProps`: called every time the store state changes. It receives the entire store state, and should return an object of data this component needs. |
| 104 | + |
| 105 | +- `mapDispatchToProps`: this parameter can either be a function, or an object. |
| 106 | + - If it’s a function, it will be called once on component creation. It will receive `dispatch` as an argument, and should return an object full of functions that use `dispatch` to dispatch actions. |
| 107 | + - If it’s an object full of action creators, each action creator will be turned into a prop function that automatically dispatches its action when called. **Note**: We recommend using this “object shorthand” form. |
| 108 | + |
| 109 | +Normally, you’ll call `connect` in this way: |
181 | 110 |
|
182 |
| -`connect` takes in two parameters. The first one allows you to define which pieces of data from the store are needed by this component. The second one allows you to indicate which actions that component might dispatch. By convention, they are called `mapStateToProps` and `mapDispatchToProps`, respectively. The return of this call is another function that accepts the component on a second call. This is an example of a pattern called [_higher order components_](https://medium.com/@franleplant/react-higher-order-components-in-depth-cf9032ee6c3e). |
| 111 | +```js |
| 112 | +const mapStateToProps = (state, ownProps) => ({ |
| 113 | + // ... computed data from state and optionally ownProps |
| 114 | +}); |
| 115 | + |
| 116 | +const mapDispatchToProps = { |
| 117 | + // ... normally is an object full of action creators |
| 118 | +}; |
| 119 | + |
| 120 | +// `connect` returns a new function that accepts the component to wrap: |
| 121 | +const connectToStore = connect( |
| 122 | + mapStateToProps, |
| 123 | + mapDispatchToProps |
| 124 | +); |
| 125 | +// and that function returns the connected, wrapper component: |
| 126 | +const ConnectedComponent = connectToStore(Component); |
| 127 | + |
| 128 | +// We normally do both in one step, like this: |
| 129 | +connect( |
| 130 | + mapStateToProps, |
| 131 | + mapDispatchToProps |
| 132 | +)(Component); |
| 133 | +``` |
183 | 134 |
|
184 | 135 | Let’s work on `<AddTodo />` first. It needs to trigger changes to the `store` to add new todos. Therefore, it needs to be able to `dispatch` actions to the store. Here’s how we do it.
|
185 | 136 |
|
|
0 commit comments