Skip to content

Commit badc15e

Browse files
committed
update zh-docs (v0.14.3+)
1 parent 4865ddf commit badc15e

17 files changed

+1233
-94
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ chrome-user-data
2525
*.sublime-workspace
2626
.idea
2727
*.iml
28+
.vscode
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
id: shallow-compare-zh-CN
3+
title: 浅比较
4+
permalink: shallow-compare-zh-CN.html
5+
prev: perf-zh-CN.html
6+
next: advanced-performance-zh-CN.html
7+
---
8+
9+
`shallowCompare` 是一个辅助函数 在以ES6类使用React时,完成和 `PureRenderMixin` 相同的功能。
10+
11+
如果你的React组件的绘制函数是 “干净的” (换句话说,它在给定的 props 和 state 下绘制相同的结果),你可以使用这个辅助函数以在某些情况下提升性能。
12+
13+
例如:
14+
15+
```js
16+
var shallowCompare = require('react-addons-shallow-compare');
17+
export class SampleComponent extends React.Component {
18+
shouldComponentUpdate(nextProps, nextState) {
19+
return shallowCompare(this, nextProps, nextState);
20+
}
21+
22+
render() {
23+
return <div className={this.props.className}>foo</div>;
24+
}
25+
}
26+
```
27+
28+
`shallowCompare` 对当前的 `props``nextProps`对象 执行一个浅的相等检查,同样对于 `state``nextState`对象。
29+
它 通过迭代比较对象的keys 并在 对象的key值不严格相等时返回false 实现此功能.
30+
31+
`shallowCompare` 返回 `true` 如果对 props 或 state的浅比较失败,因此组件应该更新。
32+
`shallowCompare` 返回 `false` 如果对 props 或 state的浅比较都通过了,因此组件不应该更新。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
id: two-way-binding-helpers-zh-CN
3+
title: 双向绑定辅助
4+
permalink: two-way-binding-helpers-zh-CN.html
5+
prev: animation-zh-CN.html
6+
next: test-utils-zh-CN.html
7+
---
8+
9+
`ReactLink` 是一个用React表达双向绑定的简单方法。
10+
11+
> 注意:
12+
>
13+
> 如果你刚学这个框架,注意 `ReactLink` 对大多数应用是不需要的,应该慎重的使用。
14+
15+
在React里,数据单向流动: 从拥有者到子级。这是因为数据只单向流动[the Von Neumann model of computing](https://en.wikipedia.org/wiki/Von_Neumann_architecture)。你可以把它想象为 “单向数据绑定”。
16+
17+
然而,有很多应用需要你去读某些数据并回流他们到你的程序。例如,当开发forms,你会常常想更新一些React `state` 当你收到用户输入的时候。或者也许你想在JavaScript完成布局并相应一些DOM元素大小的变化。
18+
19+
在React里,你可以用监听 "change" 事件来实现它,从你的数据源(通常是DOM)读取并在你的某个组件调用 `setState()` 。明确的"Closing the data flow loop" 致使了更容易理解和维护的程序。更多信息见[our forms documentation](/react/docs/forms.html).
20+
21+
双向绑定 -- 隐含的强迫DOM里的某些值总是和某些React `state` 同步 -- 简洁并支持大量多样的应用。 我们提供了 `ReactLink`:设置如上描述的通用数据回流模式的语法糖,或者 "linking" 某些数据结构到 React `state`.
22+
23+
> 注意:
24+
>
25+
> `ReactLink` 只是一层对 `onChange`/`setState()` 模式的薄包装。它没有根本性的改变你的React应用里数据如何流动。
26+
27+
## ReactLink: 之前和之后
28+
29+
这里有一个简单的 不用 `ReactLink` 的 form 例子:
30+
31+
```javascript
32+
var NoLink = React.createClass({
33+
getInitialState: function() {
34+
return {message: 'Hello!'};
35+
},
36+
handleChange: function(event) {
37+
this.setState({message: event.target.value});
38+
},
39+
render: function() {
40+
var message = this.state.message;
41+
return <input type="text" value={message} onChange={this.handleChange} />;
42+
}
43+
});
44+
```
45+
46+
这个工作的很好并且数据如何流动很清晰,然而,当有大量的 form fields时,可能会有些冗长。让我们使用 `ReactLink` 来节省我们的输入:
47+
48+
```javascript{4,9}
49+
var LinkedStateMixin = require('react-addons-linked-state-mixin');
50+
51+
var WithLink = React.createClass({
52+
mixins: [LinkedStateMixin],
53+
getInitialState: function() {
54+
return {message: 'Hello!'};
55+
},
56+
render: function() {
57+
return <input type="text" valueLink={this.linkState('message')} />;
58+
}
59+
});
60+
```
61+
62+
`LinkedStateMixin` 添加了一个 `linkState()` 方法到你的React组件。`linkState()` 返回一个 `ReactLink` 包含当前React state值的对象和一个改变它的回调函数。
63+
64+
`ReactLink` 对象可以作为props在树中上下传递,所以很容易(显示的)在深层次的组件和高层次的state之间 设置双向绑定。
65+
66+
注意 checkboxes 有一个关于他们 `value` 属性的特殊行为,这个行为是 如果checkbox被选中 值会在表单提交时被发送。 `value` 不会 checkbox 选中或是不选中时更新。对于checkboxes,你应该用`checkedLink` 代替 `valueLink`:
67+
```
68+
<input type="checkbox" checkedLink={this.linkState('booleanValue')} />
69+
```
70+
71+
## 引擎盖下
72+
73+
There are two sides to `ReactLink`: the place where you create the `ReactLink` instance and the place where you use it. To prove how simple `ReactLink` is, let's rewrite each side separately to be more explicit.
74+
75+
### ReactLink Without LinkedStateMixin
76+
77+
```javascript{5-7,9-12}
78+
var WithoutMixin = React.createClass({
79+
getInitialState: function() {
80+
return {message: 'Hello!'};
81+
},
82+
handleChange: function(newValue) {
83+
this.setState({message: newValue});
84+
},
85+
render: function() {
86+
var valueLink = {
87+
value: this.state.message,
88+
requestChange: this.handleChange
89+
};
90+
return <input type="text" valueLink={valueLink} />;
91+
}
92+
});
93+
```
94+
95+
As you can see, `ReactLink` objects are very simple objects that just have a `value` and `requestChange` prop. And `LinkedStateMixin` is similarly simple: it just populates those fields with a value from `this.state` and a callback that calls `this.setState()`.
96+
97+
### ReactLink Without valueLink
98+
99+
```javascript
100+
var LinkedStateMixin = require('react-addons-linked-state-mixin');
101+
102+
var WithoutLink = React.createClass({
103+
mixins: [LinkedStateMixin],
104+
getInitialState: function() {
105+
return {message: 'Hello!'};
106+
},
107+
render: function() {
108+
var valueLink = this.linkState('message');
109+
var handleChange = function(e) {
110+
valueLink.requestChange(e.target.value);
111+
};
112+
return <input type="text" value={valueLink.value} onChange={handleChange} />;
113+
}
114+
});
115+
```
116+
117+
The `valueLink` prop is also quite simple. It simply handles the `onChange` event and calls `this.props.valueLink.requestChange()` and also uses `this.props.valueLink.value` instead of `this.props.value`. That's it!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
id: class-name-manipulation-zh-CN
3+
title: 类名操纵
4+
permalink: class-name-manipulation-zh-CN.html
5+
prev: two-way-binding-helpers-zh-CN.html
6+
next: test-utils-zh-CN.html
7+
---
8+
9+
> NOTE:
10+
>
11+
> 此模块已被弃用; 用 [JedWatson/classnames](https://github.com/JedWatson/classnames) 替代.

docs/docs/12-context.zh-CN.md

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
id: context-zh-CN
3+
title: Context
4+
permalink: context-zh-CN.html
5+
prev: advanced-performance-zh-CN.html
6+
---
7+
8+
React最大的优势之一是他很容易从你的React组件里跟踪数据流动。当你看着一个组件,你可以很容易准确看出哪个props被传入,这让你的APP很容易推断。
9+
10+
偶尔,你想通过组件树传递数据,而不在每一级上手工下传prop,React的 "context" 让你做到这点。
11+
12+
> 注意:
13+
> 
14+
> Context是一个先进的实验性特性.这个 API 很可能在未来版本变化.
15+
>
16+
> 大多数应用将不会需要用到 context. 尤其是如果你刚开始用React,你很可能不会想用 context.使用 context 将会使你的代码很难理解因为它让数据流不清晰.它类似于在你的应用里使用全局变量传递state.
17+
>
18+
> **如果你必须使用 context ,保守的使用它**
19+
>
20+
> 不论你正在创建一个应用或者是库,试着分离你对 context 的使用到一个小区域,并尽可能避免直接使用 context API,以便在API变动时容易升级.
21+
22+
## 从树里自动传递info
23+
24+
假设你有一个这样的结构:
25+
26+
```javascript
27+
var Button = React.createClass({
28+
render: function() {
29+
return (
30+
<button style={{'{{'}}background: this.props.color}}>
31+
{this.props.children}
32+
</button>
33+
);
34+
}
35+
});
36+
37+
var Message = React.createClass({
38+
render: function() {
39+
return (
40+
<div>
41+
{this.props.text} <Button color={this.props.color}>Delete</Button>
42+
</div>
43+
);
44+
}
45+
});
46+
47+
var MessageList = React.createClass({
48+
render: function() {
49+
var color = "purple";
50+
var children = this.props.messages.map(function(message) {
51+
return <Message text={message.text} color={color} />;
52+
});
53+
return <div>{children}</div>;
54+
}
55+
});
56+
```
57+
58+
在这里例子里,我们手工穿透一个 `color` prop 以便于恰当格式化 `Button``Message` 组件.主题是一个很好的例子,当你可能想整个子树都可以访问一部分信息时(比如color). 使用 context 我们可以自动传过这个树:
59+
60+
```javascript{2-4,7,18,25-30,33}
61+
var Button = React.createClass({
62+
contextTypes: {
63+
color: React.PropTypes.string
64+
},
65+
render: function() {
66+
return (
67+
<button style={{'{{'}}background: this.context.color}}>
68+
{this.props.children}
69+
</button>
70+
);
71+
}
72+
});
73+
74+
var Message = React.createClass({
75+
render: function() {
76+
return (
77+
<div>
78+
{this.props.text} <Button>Delete</Button>
79+
</div>
80+
);
81+
}
82+
});
83+
84+
var MessageList = React.createClass({
85+
childContextTypes: {
86+
color: React.PropTypes.string
87+
},
88+
getChildContext: function() {
89+
return {color: "purple"};
90+
},
91+
render: function() {
92+
var children = this.props.messages.map(function(message) {
93+
return <Message text={message.text} />;
94+
});
95+
return <div>{children}</div>;
96+
}
97+
});
98+
```
99+
100+
通过添加 `childContextTypes``getChildContext``MessageList` ( context 提供),React下传信息到子树中的任何组件(在这个例子中, `Button`可以由定义 `contextTypes`来访问它).
101+
102+
如果 `contextTypes` 没有定义,那么 `this.context` 将是一个空对象.
103+
104+
## 父子耦合
105+
106+
Context 同样可以使你构建这样的 APT:
107+
108+
```javascript
109+
<Menu>
110+
<MenuItem>aubergine</MenuItem>
111+
<MenuItem>butternut squash</MenuItem>
112+
<MenuItem>clementine</MenuItem>
113+
</Menu>
114+
```
115+
116+
通过在 `Menu` 组件下传相关的信息,每个 `MenuItem` 可以与包含他们的 `Menu` 组件沟通.
117+
118+
**在你用这个API构建组件以前,考虑一下是否有清晰的替代方案** 我们 喜欢像这样简单的用数组传递items:
119+
120+
```javascript
121+
<Menu items={['aubergine', 'butternut squash', 'clementine']} />
122+
```
123+
124+
记住你同样可以在props里传递整个React组件,如果你想.
125+
126+
## 在生命周期方法里引用 context
127+
128+
如果 `contextTypes` 在一个组件中定义,接下来的生命周期方法会收到一个额外的参数, `context` 对象:
129+
130+
```javascript
131+
void componentWillReceiveProps(
132+
object nextProps, object nextContext
133+
)
134+
135+
boolean shouldComponentUpdate(
136+
object nextProps, object nextState, object nextContext
137+
)
138+
139+
void componentWillUpdate(
140+
object nextProps, object nextState, object nextContext
141+
)
142+
143+
void componentDidUpdate(
144+
object prevProps, object prevState, object prevContext
145+
)
146+
```
147+
148+
## 在无状态函数组件里引用 context
149+
150+
无状态函数同样能够引用 `context` 如果 `contextTypes` 被定义为函数的属性.下面的代码展示了被写为无状态函数组件的 `Button` 组件.
151+
152+
```javascript
153+
function Button(props, context) {
154+
return (
155+
<button style={{'{{'}}background: context.color}}>
156+
{props.children}
157+
</button>
158+
);
159+
}
160+
Button.contextTypes = {color: React.PropTypes.string};
161+
```
162+
163+
## 什么时候不用 context
164+
165+
正像全局变量是在写清晰代码时最好要避免的,你应该在大多数情况下避免使用context. 特别是,在用它来"节省输入"和代替显示传入props时要三思.
166+
167+
context最好的使用场景是隐式的传入登录的用户,当前的语言,或者主题信息.要不然所有这些可能就是全局变量,但是context让你限定他们到一个单独的React树里.
168+
169+
不要用context在组件里传递你的模型数据.通过树显示的传递你的数据更容易理解.使用context使你的组件更耦合和不可复用,因为 依赖于他们在哪里渲染,他们会表现不同的行为.
170+
171+
## 已知的限制
172+
173+
如果一个由组件提供的context值变动,使用那个值的子级不会更新,如果一个直接的父级从 `shouldComponentUpdate` 返回 `false` .详见 issue [#2517](https://github.com/facebook/react/issues/2517) .

docs/docs/conferences.zh-CN.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
id: conferences-zh-CN
3+
title: 会议
4+
permalink: conferences-zh-CN.html
5+
prev: thinking-in-react-zh-CN.html
6+
next: videos-zh-CN.html
7+
---
8+
9+
### React.js Conf 2015
10+
一月 28 & 29
11+
12+
[Website](http://conf.reactjs.com/) - [Schedule](http://conf.reactjs.com/schedule.html) - [Videos](https://www.youtube-nocookie.com/playlist?list=PLb0IAmt7-GS1cbw4qonlQztYV1TAW0sCr)
13+
14+
<iframe width="650" height="315" src="//www.youtube-nocookie.com/embed/KVZ-P-ZI6W4?list=PLb0IAmt7-GS1cbw4qonlQztYV1TAW0sCr" frameborder="0" allowfullscreen></iframe>
15+
16+
### ReactEurope 2015
17+
七月 2 & 3
18+
19+
[Website](http://www.react-europe.org/) - [Schedule](http://www.react-europe.org/#schedule)
20+
21+
### Reactive 2015
22+
十一月 2-4
23+
24+
[Website](https://reactive2015.com/) - [Schedule](https://reactive2015.com/schedule_speakers.html#schedule)
25+
26+
### ReactEurope 2016
27+
六月 2 & 3
28+
29+
[Website](http://www.react-europe.org/) - [Schedule](http://www.react-europe.org/#schedule)

0 commit comments

Comments
 (0)