Skip to content

Commit 04e7bb5

Browse files
橡树上QC-L
橡树上
authored andcommitted
docs(cn): translate content/docs/context.md into Chinese (#87)
1 parent 719e8a6 commit 04e7bb5

9 files changed

+93
-100
lines changed

content/docs/context.md

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

examples/context/motivation-problem.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ class App extends React.Component {
66

77
function Toolbar(props) {
88
// highlight-range{1-4,7}
9-
// The Toolbar component must take an extra "theme" prop
10-
// and pass it to the ThemedButton. This can become painful
11-
// if every single button in the app needs to know the theme
12-
// because it would have to be passed through all components.
9+
// Toolbar 组件接受一个额外的“theme”属性,然后传递给 ThemedButton 组件。
10+
// 如果应用中每一个单独的按钮都需要知道 theme 的值,这会是件很麻烦的事,
11+
// 因为必须将这个值层层传递所有组件。
1312
return (
1413
<div>
1514
<ThemedButton theme={props.theme} />

examples/context/motivation-solution.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// highlight-range{1-4}
2-
// Context lets us pass a value deep into the component tree
3-
// without explicitly threading it through every component.
4-
// Create a context for the current theme (with "light" as the default).
2+
// Context 可以让我们无须明确地传遍每一个组件,就能将值深入传递进组件树。
3+
// 为当前的 theme 创建一个 context(“light”为默认值)。
54
const ThemeContext = React.createContext('light');
65

76
class App extends React.Component {
87
render() {
98
// highlight-range{1-3,5}
10-
// Use a Provider to pass the current theme to the tree below.
11-
// Any component can read it, no matter how deep it is.
12-
// In this example, we're passing "dark" as the current value.
9+
// 使用一个 Provider 来将当前的 theme 传递给以下的组件树。
10+
// 无论多深,任何组件都能读取这个值。
11+
// 在这个例子中,我们将 “dark” 作为当前的值传递下去。
1312
return (
1413
<ThemeContext.Provider value="dark">
1514
<Toolbar />
@@ -19,8 +18,7 @@ class App extends React.Component {
1918
}
2019

2120
// highlight-range{1,2}
22-
// A component in the middle doesn't have to
23-
// pass the theme down explicitly anymore.
21+
// 中间的组件再也不必指明往下传递 theme 了。
2422
function Toolbar(props) {
2523
return (
2624
<div>
@@ -31,9 +29,9 @@ function Toolbar(props) {
3129

3230
class ThemedButton extends React.Component {
3331
// highlight-range{1-3,6}
34-
// Assign a contextType to read the current theme context.
35-
// React will find the closest theme Provider above and use its value.
36-
// In this example, the current theme is "dark".
32+
// 指定 contextType 读取当前的 theme context
33+
// React 会往上找到最近的 theme Provider,然后使用它的值。
34+
// 在这个例子中,当前的 theme 值为 “dark”。
3735
static contextType = ThemeContext;
3836
render() {
3937
return <Button theme={this.context} />;

examples/context/multiple-contexts.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Theme context, default to light theme
1+
// Theme context,默认的 theme 是 “light” 值
22
const ThemeContext = React.createContext('light');
33

4-
// Signed-in user context
4+
// 用户登录 context
55
const UserContext = React.createContext({
66
name: 'Guest',
77
});
@@ -10,7 +10,7 @@ class App extends React.Component {
1010
render() {
1111
const {signedInUser, theme} = this.props;
1212

13-
// App component that provides initial context values
13+
// 提供初始 context 值的 App 组件
1414
// highlight-range{2-3,5-6}
1515
return (
1616
<ThemeContext.Provider value={theme}>
@@ -31,7 +31,7 @@ function Layout() {
3131
);
3232
}
3333

34-
// A component may consume multiple contexts
34+
// 一个组件可能会消费多个 context
3535
function Content() {
3636
// highlight-range{2-10}
3737
return (

examples/context/theme-detailed-app.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ThemeContext, themes} from './theme-context';
22
import ThemedButton from './themed-button';
33

4-
// An intermediate component that uses the ThemedButton
4+
// 一个使用 ThemedButton 的中间组件
55
function Toolbar(props) {
66
return (
77
<ThemedButton onClick={props.changeTheme}>
@@ -28,10 +28,9 @@ class App extends React.Component {
2828
}
2929

3030
render() {
31-
//highlight-range{1-3}
32-
// The ThemedButton button inside the ThemeProvider
33-
// uses the theme from state while the one outside uses
34-
// the default dark theme
31+
// highlight-range{1-3}
32+
// 在 ThemeProvider 内部的 ThemedButton 按钮组件使用 state 中的 theme 值,
33+
// 而外部的组件使用默认的 theme 值
3534
//highlight-range{3-5,7}
3635
return (
3736
<Page>

examples/context/theme-detailed-theme-context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export const themes = {
1111

1212
// highlight-range{1-3}
1313
export const ThemeContext = React.createContext(
14-
themes.dark // default value
14+
themes.dark // 默认值
1515
);

examples/context/updating-nested-context-app.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class App extends React.Component {
1515
};
1616

1717
// highlight-range{1-2,5}
18-
// State also contains the updater function so it will
19-
// be passed down into the context provider
18+
// State 也包含了更新函数,因此它会被传递进 context provider。
2019
this.state = {
2120
theme: themes.light,
2221
toggleTheme: this.toggleTheme,
@@ -25,7 +24,7 @@ class App extends React.Component {
2524

2625
render() {
2726
// highlight-range{1,3}
28-
// The entire state is passed to the provider
27+
// 整个 state 都被传递进 provider
2928
return (
3029
<ThemeContext.Provider value={this.state}>
3130
<Content />

examples/context/updating-nested-context-context.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Make sure the shape of the default value passed to
2-
// createContext matches the shape that the consumers expect!
1+
// 确保传递给 createContext 的默认值数据结构是调用的组件(consumers)所能匹配的!
32
// highlight-range{2-3}
43
export const ThemeContext = React.createContext({
54
theme: themes.dark,

examples/context/updating-nested-context-theme-toggler-button.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import {ThemeContext} from './theme-context';
22

33
function ThemeTogglerButton() {
44
// highlight-range{1-2,5}
5-
// The Theme Toggler Button receives not only the theme
6-
// but also a toggleTheme function from the context
5+
// Theme Toggler 按钮不仅仅只获取 theme 值,它也从 context 中获取到一个 toggleTheme 函数
76
return (
87
<ThemeContext.Consumer>
98
{({theme, toggleTheme}) => (

0 commit comments

Comments
 (0)