Skip to content

Commit 100383a

Browse files
authored
docs(cn): translate content/blog/2018-03-27-update-on-async-rend… (#282)
* docs(cn): translate content/blog/2018-03-27-update-on-async-rendering.md into Chinese * according to review suggestions, correct the translation. * modify translation
1 parent 5d53fd9 commit 100383a

12 files changed

+136
-136
lines changed

content/blog/2018-03-27-update-on-async-rendering.md

+98-98
Large diffs are not rendered by default.

examples/update-on-async-rendering/adding-event-listeners-after.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ class ExampleComponent extends React.Component {
77
// highlight-line
88
// highlight-range{1-18}
99
componentDidMount() {
10-
// Event listeners are only safe to add after mount,
11-
// So they won't leak if mount is interrupted or errors.
10+
// 事件监听器只有在挂载后添加才是安全的,
11+
// 因此,如果挂载中断或错误,它们不会泄漏。
1212
this.props.dataSource.subscribe(
1313
this.handleSubscriptionChange
1414
);
1515

16-
// External values could change between render and mount,
17-
// In some cases it may be important to handle this case.
16+
// 外部值可能在渲染和挂载期间改变,
17+
// 在某些情况下,处理这种情况很重要。
1818
if (
1919
this.state.subscribedValue !==
2020
this.props.dataSource.value

examples/update-on-async-rendering/adding-event-listeners-before.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ExampleComponent extends React.Component {
66
subscribedValue: this.props.dataSource.value,
77
});
88

9-
// This is not safe; it can leak!
9+
// 这是不安全的,它会导致内存泄漏!
1010
this.props.dataSource.subscribe(
1111
this.handleSubscriptionChange
1212
);

examples/update-on-async-rendering/adding-event-listeners-create-subscription.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {createSubscription} from 'create-subscription';
22

33
const Subscription = createSubscription({
44
getCurrentValue(sourceProp) {
5-
// Return the current value of the subscription (sourceProp).
5+
// 返回订阅的当前值(sourceProp)。
66
return sourceProp.value;
77
},
88

@@ -11,19 +11,19 @@ const Subscription = createSubscription({
1111
callback(sourceProp.value);
1212
}
1313

14-
// Subscribe (e.g. add an event listener) to the subscription (sourceProp).
15-
// Call callback(newValue) whenever a subscription changes.
14+
// 订阅(例如:向订阅(sourceProp)添加事件监听器。
15+
// 每当订阅发生变化时,调用回调函数(新值)。
1616
sourceProp.subscribe(handleSubscriptionChange);
1717

18-
// Return an unsubscribe method.
18+
// 返回取消订阅方法。
1919
return function unsubscribe() {
2020
sourceProp.unsubscribe(handleSubscriptionChange);
2121
};
2222
},
2323
});
2424

25-
// Rather than passing the subscribable source to our ExampleComponent,
26-
// We could just pass the subscribed value directly:
25+
// 我们可以直接传递订阅的值,
26+
// 而不是将可订阅的源传递给我们的 ExampleComponent:
2727
<Subscription source={dataSource}>
2828
{value => <ExampleComponent subscribedValue={value} />}
2929
</Subscription>;

examples/update-on-async-rendering/fetching-external-data-after.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class ExampleComponent extends React.Component {
2222

2323
render() {
2424
if (this.state.externalData === null) {
25-
// Render loading state ...
25+
// 渲染加载状态 ...
2626
} else {
27-
// Render real UI ...
27+
// 渲染真实 UI ...
2828
}
2929
}
3030
}

examples/update-on-async-rendering/fetching-external-data-before.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class ExampleComponent extends React.Component {
2222

2323
render() {
2424
if (this.state.externalData === null) {
25-
// Render loading state ...
25+
// 渲染加载状态 ...
2626
} else {
27-
// Render real UI ...
27+
// 渲染真实 UI ...
2828
}
2929
}
3030
}

examples/update-on-async-rendering/react-dom-properties-before-update-after.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ class ScrollingList extends React.Component {
33

44
// highlight-range{1-10}
55
getSnapshotBeforeUpdate(prevProps, prevState) {
6-
// Are we adding new items to the list?
7-
// Capture the scroll position so we can adjust scroll later.
6+
// 我们正在向列表中添加新项吗?
7+
// 捕获滚动位置,以便我们稍后可以调整滚动位置。
88
if (prevProps.list.length < this.props.list.length) {
99
return (
1010
this.listRef.scrollHeight - this.listRef.scrollTop
@@ -15,9 +15,9 @@ class ScrollingList extends React.Component {
1515

1616
// highlight-range{1-8}
1717
componentDidUpdate(prevProps, prevState, snapshot) {
18-
// If we have a snapshot value, we've just added new items.
19-
// Adjust scroll so these new items don't push the old ones out of view.
20-
// (snapshot here is the value returned from getSnapshotBeforeUpdate)
18+
// 如果我们刚刚添加了新项,并且有了快照值。
19+
// 调整滚动位置,以便这些新项不会把旧项挤出视图。
20+
// (此处的快照是从 getSnapshotBeforeUpdate 返回的值)
2121
if (snapshot !== null) {
2222
this.listRef.scrollTop =
2323
this.listRef.scrollHeight - snapshot;
@@ -27,7 +27,7 @@ class ScrollingList extends React.Component {
2727
render() {
2828
return (
2929
<div ref={this.setListRef}>
30-
{/* ...contents... */}
30+
{/* ...内容... */}
3131
</div>
3232
);
3333
}

examples/update-on-async-rendering/react-dom-properties-before-update-before.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class ScrollingList extends React.Component {
44

55
// highlight-range{1-8}
66
componentWillUpdate(nextProps, nextState) {
7-
// Are we adding new items to the list?
8-
// Capture the scroll position so we can adjust scroll later.
7+
// 我们正在向列表中添加新项吗?
8+
// 捕获滚动位置,以便我们稍后可以调整滚动位置。
99
if (this.props.list.length < nextProps.list.length) {
1010
this.previousScrollOffset =
1111
this.listRef.scrollHeight - this.listRef.scrollTop;
@@ -14,8 +14,8 @@ class ScrollingList extends React.Component {
1414

1515
// highlight-range{1-10}
1616
componentDidUpdate(prevProps, prevState) {
17-
// If previousScrollOffset is set, we've just added new items.
18-
// Adjust scroll so these new items don't push the old ones out of view.
17+
// 如果我们刚刚添加了新项,并且设置了 previousScrollOffset
18+
// 调整滚动位置,以便这些新项不会把旧项挤出视图。
1919
if (this.previousScrollOffset !== null) {
2020
this.listRef.scrollTop =
2121
this.listRef.scrollHeight -
@@ -27,7 +27,7 @@ class ScrollingList extends React.Component {
2727
render() {
2828
return (
2929
<div ref={this.setListRef}>
30-
{/* ...contents... */}
30+
{/* ...内容... */}
3131
</div>
3232
);
3333
}

examples/update-on-async-rendering/updating-external-data-when-props-change-after.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ class ExampleComponent extends React.Component {
66

77
// highlight-range{1-13}
88
static getDerivedStateFromProps(props, state) {
9-
// Store prevId in state so we can compare when props change.
10-
// Clear out previously-loaded data (so we don't render stale stuff).
9+
// 保存 prevId state 中,以便我们在 props 变化时进行对比。
10+
// 清除之前加载的数据(这样我们就不会渲染旧的内容)。
1111
if (props.id !== state.prevId) {
1212
return {
1313
externalData: null,
1414
prevId: props.id,
1515
};
1616
}
1717

18-
// No state update necessary
18+
// 无需更新 state
1919
return null;
2020
}
2121

@@ -38,9 +38,9 @@ class ExampleComponent extends React.Component {
3838

3939
render() {
4040
if (this.state.externalData === null) {
41-
// Render loading state ...
41+
// 渲染加载状态 ...
4242
} else {
43-
// Render real UI ...
43+
// 渲染真实 UI ...
4444
}
4545
}
4646

examples/update-on-async-rendering/updating-external-data-when-props-change-before.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class ExampleComponent extends React.Component {
2424

2525
render() {
2626
if (this.state.externalData === null) {
27-
// Render loading state ...
27+
// 渲染加载状态 ...
2828
} else {
29-
// Render real UI ...
29+
// 渲染真实 UI ...
3030
}
3131
}
3232

examples/update-on-async-rendering/updating-state-from-props-after.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// After
22
class ExampleComponent extends React.Component {
3-
// Initialize state in constructor,
4-
// Or with a property initializer.
3+
// 在构造函数中初始化 state
4+
// 或者使用属性初始化器。
55
// highlight-range{1-4}
66
state = {
77
isScrollingDown: false,
@@ -17,7 +17,7 @@ class ExampleComponent extends React.Component {
1717
};
1818
}
1919

20-
// Return null to indicate no change to state.
20+
// 返回 null 表示无需更新 state
2121
return null;
2222
}
2323
}

examples/update-on-async-rendering/using-react-lifecycles-compat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {polyfill} from 'react-lifecycles-compat';
55
class ExampleComponent extends React.Component {
66
// highlight-next-line
77
static getDerivedStateFromProps(props, state) {
8-
// Your state update logic here ...
8+
// 此处为 state 更新的逻辑 ...
99
}
1010
}
1111

12-
// Polyfill your component to work with older versions of React:
12+
// polyfill 你的组件,以便兼容旧版本的 React
1313
// highlight-next-line
1414
polyfill(ExampleComponent);
1515

0 commit comments

Comments
 (0)