You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial/9-redux/README.md
+29-27Lines changed: 29 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
# 9 - Redux
2
2
3
-
In this chapter (which is the most difficult so far) we will be adding [Redux](http://redux.js.org/)to our application and will hook it up with React. Redux manages the state of your application. It is composed of a **store** which is a plain JavaScript object representing the state of your app, **actions**which are typically triggered by users, and **reducers**which can be seen as action handlers. Reducers affect your application state (the *store*), and when the application state is modified, things happen in your app. A good visual demonstration of Redux can be found <ahref="http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#/9">here</a>.
In order to demonstrate how to use Redux in the simplest possible way, our app will consist of a message and a button. The message says whether the dog has barked or not (it initially hasn't), and the button makes the dog bark, which should update the message.
Here we define an action type,`MAKE_BARK`, and a function (also known as *action creator*) that triggers a `MAKE_BARK` action called `makeBark`. Both are exported because we'll need them both in other files. This action implements the [Flux Standard Action](https://github.com/acdlite/flux-standard-action)model, which is why it has `type`and`payload`attributes.
Here we define the initial state of our app, which is an object containing the `hasBarked`property set to `false`, and the `dogReducer`, which is the function responsible for altering the state based on which action happened. The state cannot be modified in this function, a brand new state object must be returned.
-We are now going to modify `app.jsx`to create the *store*. You can replace the entire content of that file by the following:
48
+
-修改 `app.jsx`创建一个 *store*,替换成以下内容:
49
49
50
50
```javascript
51
51
importReactfrom'react';
@@ -71,19 +71,20 @@ ReactDOM.render(
71
71
);
72
72
```
73
73
74
-
Our store is created by the Redux function `createStore`, pretty explicit. The store object is assembled by combining all our reducers (in our case, only one) using Redux's `combineReducers`function. Each reducer is named here, and we'll name ours `dog`.
Now we are going to hook up Redux with React using `react-redux`. In order for `react-redux` to pass the store to our React app, it needs to wrap the entire app in a `<Provider>`component. This component must have a single child, so we created a `<div>`, and this `<div>` contains the 2 main elements of our app, a `BarkMessage`and a `BarkButton`.
As you can tell in the `import` section, `BarkMessage`and`BarkButton`are imported from a `containers`folder. Now is a good time to introduce the concept of **Components** and **Containers**.
*Components* are *dumb* React components, in a sense that they don't know anything about the Redux state. *Containers* are *smart* components that know about the state and that we are going to *connect* to our dumb components.
These are examples of *dumb* components. They are logic-less, and just show whatever they are asked to show via React **props**. The main difference between `button.jsx`and`message.jsx`is that `Button`contains an **action** in its props. That action is bound on the `onClick`event. In the context of our app, the `Button`label is never going to change, however, the `Message`component is going to reflect the state of our app, and will vary based on the state.
Again, *components* don't know anything about Redux **actions**or the **state**of our app, which is why we are going to create smart **containers** that will feed the proper *actions* and *data* to these 2 dumb components.
@@ -149,10 +149,12 @@ const mapStateToProps = state => ({
149
149
exportdefaultconnect(mapStateToProps)(Message);
150
150
```
151
151
152
-
`BarkButton` will hook up `Button` with the `makeBark` action and Redux's `dispatch` method, and `BarkMessage` will hook up the app state with `Message`. When the state changes, `Message` will now automatically re-render with the proper `message` prop. These connections are done via the `connect` function of `react-redux`.
- 现在可以执行 `yarn start` ,并打开 `index.html` 了。可以看到 "The dog did not bark" 的消息以及一个按钮。点击按钮后消息会变成 "The dog barked"。
153
155
154
-
- You can now run `yarn start` and open `index.html`. You should see "The dog did not bark" and a button. When you click the button, the message should show "The dog barked".
0 commit comments