1
1
// 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
+ // 원하는 값을 컴포넌트 트리 깊숙한 곳까지 보낼 수 있습니다 .
4
+ // 라이트(light)를 기본값으로 하는 테마 context를 만들어 봅시다 .
5
5
const ThemeContext = React . createContext ( 'light' ) ;
6
6
7
7
class App extends React . Component {
8
8
render ( ) {
9
9
// 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 .
10
+ // Provider를 이용해 하위 트리에 테마 값을 보내줍니다 .
11
+ // 아무리 깊숙히 있어도, 모든 컴포넌트가 이 값을 읽을 수 있습니다 .
12
+ // 아래 예시에서는 다크(dark)를 현재 선택된 테마 값으로 보내고 있습니다 .
13
13
return (
14
14
< ThemeContext . Provider value = "dark" >
15
15
< Toolbar />
@@ -18,9 +18,8 @@ class App extends React.Component {
18
18
}
19
19
}
20
20
21
- // highlight-range{1,2}
22
- // A component in the middle doesn't have to
23
- // pass the theme down explicitly anymore.
21
+ // highlight-next-line
22
+ // 이젠 중간에 있는 컴포넌트가 일일히 테마를 넘겨줄 필요가 없습니다.
24
23
function Toolbar ( props ) {
25
24
return (
26
25
< div >
@@ -31,9 +30,9 @@ function Toolbar(props) {
31
30
32
31
class ThemedButton extends React . Component {
33
32
// 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" .
33
+ // 현재 선택된 테마 값을 읽기 위해 contextType을 지정합니다 .
34
+ // React는 가장 가까이 있는 테마 Provider를 찾아 그 값을 사용할 것입니다 .
35
+ // 이 예시에서 현재 선택된 테마는 다크입니다 .
37
36
static contextType = ThemeContext ;
38
37
render ( ) {
39
38
return < Button theme = { this . context } /> ;
0 commit comments