Skip to content

Commit 6bfce9f

Browse files
committed
翻译[11_src/src/home.jsx]、[11_src/src/server.js]完成
1 parent 0634f04 commit 6bfce9f

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

11_src/src/home.jsx

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// 教程 12 - Provider-and-connect.js
22

3-
// 我们的教程快结束了, 离对 Redux 有一个好的认识只差一步: 如何从 store 中的 state 读取, 和以及如何派发 actions?
3+
// 我们的教程快结束了, 离对 Redux 有一个好的认识只差一步:
4+
// 如何从 store 中的 state 读取, 和以及如何派发 actions?
45

56
// 这两个问题可以用 react-redux 的 connect 绑定一并解决。
67

78
// 如我们之前解释的, 当使用 Provider 组件的时候, 我们允许应用中所有组件访问 Redux。
89
// 但这个访问只能通过没有配备说明文档的功能 "React 的 context" 来完成。
9-
// 为了避开这如"黑魔法"般的 React API , React-Redux 暴露了一个函数, 这样你就能在组件的 类中使用它
10+
// 为了避开这如"黑魔法"般的 React API , React-Redux 暴露了一个函数, 这样你就能在组件的类中使用它
1011

1112
// 我们讨论的这个函数就是 "connect" , 它能让我们用 Redux 的 store 字面值与组件连接上。
1213
// 这样一来, 它就能让你 store 中的 dispatch 函数通过组件的 props 传递,
@@ -57,7 +58,7 @@ import * as actionCreators from './action-creators'
5758

5859
class Home extends React.Component {
5960
onTimeButtonClick (delay) {
60-
// 当用户点击这个按钮的时候, 该句柄会派发一个action来响应
61+
// 当用户点击这个按钮的时候, 该句柄会派发一个 action 来响应
6162
// 这里我们使用的 dispatch 函数由 props 中的 connect "自动"提供。
6263
// 还有多种其他的方法来调用已经绑定到 dispatch 中的 actionCreator, 这意味着可以向 "connect" 提供第二个参数:
6364
// https://github.com/rackt/react-redux/blob/v4.0.0/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options
@@ -149,26 +150,29 @@ export default ConnectedHome
149150
export default class MyClass {}
150151
*/
151152

152-
// Applying this syntax to redux connect, you can replace:
153+
// 在 Redux 的 connect 中使用修饰器语法, 可以用:
153154

154155
/*
155-
let mapStateToProps = (state) => { ... }
156-
class MyClass {}
157-
export default connect(mapStateToProps)(MyClass)
158-
*/
156+
let mapStateToProps = (state) => { ... }
157+
@connect(mapStateToProps)
158+
export default class MyClass {}
159+
*/
159160

160-
// by:
161+
// 替换:
161162

162163
/*
163164
let mapStateToProps = (state) => { ... }
164-
@connect(mapStateToProps)
165-
export default class MyClass {}
165+
class MyClass {}
166+
export default connect(mapStateToProps)(MyClass)
166167
*/
167168

168-
// As you can see the application of the HOC function on the component class is now made implicit ( @connect(mapStateToProps) )
169-
// instead of calling it ourselves ( @connect(mapStateToProps)(Myclass) ). Some find this approach more elegant, others
170-
// dislike the fact that it's hiding what is really happening and many just don't get how decorators works. Knowing all that
171-
// and remembering that decorators are still experimental, you can now decide by youselves which "connect" usage you
172-
// prefer and you won't be suprised to find both syntax in the many articles, tutorials, starter kits, etc. out there.
173169

174-
// Go to ./12_final-words.js for our last advice about what to do now...
170+
// 如你所见, 组件类中的高阶组件函数写法更加含蓄了:
171+
// @connect(mapStateToProps)
172+
// 而不是我们自己调用它:
173+
// @connect(mapStateToProps)(Myclass)
174+
// 有些人觉得这样写更优雅, 但有的人不喜欢, 因为这样隐藏了背后真正发生的事情, 或是不理解修饰器是如何工作的。
175+
// 记住, 修饰器目前为实验性功能, 如何使用 "connect" 取决于你自己,
176+
// 在别处的文章、教程、项目模版中, 常可以看到这两种语法。
177+
178+
// 转到 ./12_final-words.js 去看我们最后的建议...

11_src/src/server.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
// Tutorial 12 - Provider-and-connect.js
1+
// 教程 12 - Provider-and-connect.js
22

3-
// Hi there! So you're ready to play with Redux in a React app?
3+
// 终于到这里了! 准备好在 React 应用中使用 Redux 了吗?
44

5-
// The application you're about to see could not be more minimalist (from a product point of view
6-
// and from a design point of view - sorry for the black and white)... We'll only focus
7-
// on the use of 2 main bindings of react-redux (https://github.com/gaearon/react-redux):
8-
// 1) the Provider component
9-
// 2) the connect function
5+
// 你看到的这个应用程序例子已经精简的不能再精简了(对一个产品和设计来说的话)...
6+
// 我们只关注了使用 react-redux 中2个主要的绑定 (https://github.com/gaearon/react-redux):
7+
// 1) Provider 组件
8+
// 2) connect 函数
109

11-
// But before we get to that, let's see the basic setup of this application and how it
12-
// will be served to browser...
10+
// 但在开始下一步之前, 先看看为了使应用程序能在浏览器中访问到, 需要如何做一些基本设置。
1311

14-
// We won't use Express (http://expressjs.com/) in this app since we don't really need
15-
// it to serve a simple html page.
12+
// 该应用中我们没有用 Express (http://expressjs.com/), 因为在一个如此简单的 HTML 页面中, 我们都不需要用到它。
1613

17-
// "http" module will be used to create the http server
14+
// 这里我们使用 http 模块来创建一个 http 服务器
1815
import http from 'http'
1916
import React from 'react'
2017

21-
// We create our main application server here. It will serve the same page on all URIs
22-
// so you won't find any route specific logic below (except for rejecting favicon request)
18+
// 这里创建应用程序主服务器。 它会伺服所有的 URI 到同一个页面上,
19+
// 所以这里并没有具体的路由逻辑, 除了一个拒绝 favicon 请求的代码。
2320
var server = http.createServer(function(req, res) {
2421

25-
// Forget this, it's just to avoid serving anything when the browser automatically
26-
// requests favicon (if not, this server would send back an html page).
22+
// 别管这个, 它仅仅用来取消浏览器对 favicon 的自动请求,
23+
// 如果不这样做的话, 该服务器会返回一个 HTML 页面。
2724
if (req.url.match('favicon.ico')) {
2825
return res.end()
2926
}
3027

31-
// And of course, here is our Application HTML that we're sending back to the browser.
32-
// Nothing special here except the URI of our application JS bundle that points to our
28+
// 当然还有, 这里是我们应用程序返回给浏览器的 HTML
29+
// 没什么特别的, 除了将应用程序的 JS 包地址指定到
3330
// webpack dev server (located at http://localhost:5051)
3431
res.write(
3532
`<!DOCTYPE html>
@@ -49,6 +46,6 @@ var server = http.createServer(function(req, res) {
4946

5047
export default server
5148

52-
// Go to ./index.jsx, where our app is initialized. For those of you who are not familiar with webpack,
53-
// index.jsx is defined as the entry point (the first file) of our JS bundle (in 12_src/webpack.config.js)
54-
// and is automatically executed when the JS bundle is loaded in our browser.
49+
// 转到 ./index.jsx, 那是我们应用程序初始化的地方。
50+
// 为了让对 webpack 不熟悉的人更容易理解, 其实 index.jsx 是被定义(在 12_src/webpack.config.js中) 为 JS 包的入口 (首个文件)
51+
// JS 包被载入到浏览器后, 它将被自动运行。

0 commit comments

Comments
 (0)