|
1 | | -// Tutorial 12 - Provider-and-connect.js |
| 1 | +// 教程 12 - Provider-and-connect.js |
2 | 2 |
|
3 | | -// Our tutorial is almost over and the only missing piece to leave you with a good overview of Redux is: |
4 | | -// How do we read from our store's state and how do we dispatch actions? |
| 3 | +// 到这为止我们的教程就基本上结束了, 关于Redux唯一剩下的问题 |
| 4 | +// 就是我们如何能读取store里面的state以及如何进行action的分发。 |
5 | 5 |
|
6 | | -// Both of these questions can be answered using a single react-redux's binding: connect. |
| 6 | +// 这两个问题都能够使用connect去解决。 |
7 | 7 |
|
8 | | -// As we previously explained, when using the Provider component we allow all components of our app to |
9 | | -// access Redux. But this access can only be made through the undocumented feature "React's context". To |
10 | | -// avoid asking you to use such a "dark" React API, React-Redux is exposing a function that you can use |
11 | | -// on a component class. |
| 8 | +// 正如我们前面所讲解的, 当我们使用Provider组件时, 我们允许我们应用中的所有组件访问Redux. |
| 9 | +// 但是这种访问只能使用不正式的特性React's context来实现, |
| 10 | +// 为了避免这种"黑科技"式 (不规范) 的API调用, |
| 11 | +// React-Redux为我们暴露了一个组件中的函数让我们可以使用。 |
12 | 12 |
|
13 | | -// The function we're talking about is "connect" and it allows to literally connect your component with your Redux's store. |
14 | | -// By doing so, it provides your store's dispatch function through a component's prop and also adds any |
15 | | -// properties you want to expose as part of your store's state. |
| 13 | +// 这个函数就是Connect, 它让我们可以实现一个组件和Redux store的绑定, |
| 14 | +// 通过这种绑定可以让store通过组件的属性(prop)分发函数, |
| 15 | +// 也可以根据我们自己的需要增加任何需要暴露的属性作为store里面state的一部分。 |
16 | 16 |
|
17 | | -// Using "connect", you'll turn a dumb component into a smart component with very little code overhead |
| 17 | +// 使用了Connect, 你可以通过添加很少的代码让一个组件变得更"聪明", |
18 | 18 | // (https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0). |
19 | 19 |
|
20 | 20 | // "connect" is a function that takes as parameters few mapping functions and that returns a function expecting |
@@ -104,8 +104,7 @@ class Home extends React.Component { |
104 | 104 | } |
105 | 105 | } |
106 | 106 |
|
107 | | -// This is our select function that will extract from the state the data slice we want to expose |
108 | | -// through props to our component. |
| 107 | +// 这是我们的select函数, 它会把我们需要在属性(prop)中对我们的组件暴露的数据从state中抽离出来 |
109 | 108 | const mapStateToProps = (state/*, props*/) => { |
110 | 109 | return { |
111 | 110 | frozen: state._time.frozen, |
@@ -148,22 +147,22 @@ export default ConnectedHome |
148 | 147 | export default somedecorator(MyClass) |
149 | 148 | */ |
150 | 149 |
|
151 | | -// You can write: |
| 150 | +// 你可以这么写: |
152 | 151 |
|
153 | 152 | /* |
154 | 153 | @somedecorator |
155 | 154 | export default class MyClass {} |
156 | 155 | */ |
157 | 156 |
|
158 | | -// Applying this syntax to redux connect, you can replace: |
| 157 | +// 通过这种特性使用redux connect, 我们可以把如下代码: |
159 | 158 |
|
160 | 159 | /* |
161 | 160 | let mapStateToProps = (state) => { ... } |
162 | 161 | class MyClass {} |
163 | 162 | export default connect(mapStateToProps)(MyClass) |
164 | 163 | */ |
165 | 164 |
|
166 | | -// by: |
| 165 | +// 替换成: |
167 | 166 |
|
168 | 167 | /* |
169 | 168 | let mapStateToProps = (state) => { ... } |
|
0 commit comments