Skip to content

Commit ea6fb5c

Browse files
Translate createContext (#638)
* Translate createContext * fix some styles * fix translations * fix quote style * Restore link that was accidentally removed * Restore more links * Fix capitalization problems --------- Co-authored-by: Soichiro Miki <[email protected]>
1 parent fed5cdc commit ea6fb5c

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

Diff for: src/content/reference/react/createContext.md

+36-36
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: createContext
44

55
<Intro>
66

7-
`createContext` lets you create a [context](/learn/passing-data-deeply-with-context) that components can provide or read.
7+
`createContext` は、コンポーネントが提供または読み取りできる[コンテクスト](/learn/passing-data-deeply-with-context)を作成するための関数です。
88

99
```js
1010
const SomeContext = createContext(defaultValue)
@@ -16,38 +16,38 @@ const SomeContext = createContext(defaultValue)
1616

1717
---
1818

19-
## Reference {/*reference*/}
19+
## リファレンス {/*reference*/}
2020

2121
### `createContext(defaultValue)` {/*createcontext*/}
2222

23-
Call `createContext` outside of any components to create a context.
23+
`createContext` をコンポーネントの外部で呼び出してコンテクストを作成します。
2424

2525
```js
2626
import { createContext } from 'react';
2727

2828
const ThemeContext = createContext('light');
2929
```
3030

31-
[See more examples below.](#usage)
31+
[さらに例を見る](#usage)
3232

33-
#### Parameters {/*parameters*/}
33+
#### 引数 {/*parameters*/}
3434

35-
* `defaultValue`: The value that you want the context to have when there is no matching context provider in the tree above the component that reads context. If you don't have any meaningful default value, specify `null`. The default value is meant as a "last resort" fallback. It is static and never changes over time.
35+
* `defaultValue`: コンポーネントがコンテクストを読み取るときに、その上のツリー内で対応するコンテクストプロバイダがない場合にコンテクストが持つ値です。デフォルト値が必要ない場合は `null` を指定します。デフォルト値は「最後の手段」として使われるように意図されています。これは静的な値であり、時間が経過しても変化しません。
3636

37-
#### Returns {/*returns*/}
37+
#### 返り値 {/*returns*/}
3838

39-
`createContext` returns a context object.
39+
`createContext` はコンテクストオブジェクトを返します。
4040

41-
**The context object itself does not hold any information.** It represents _which_ context other components read or provide. Typically, you will use [`SomeContext.Provider`](#provider) in components above to specify the context value, and call [`useContext(SomeContext)`](/reference/react/useContext) in components below to read it. The context object has a few properties:
41+
**コンテクストオブジェクト自体は情報を持っていません**。他のコンポーネントが*どの*コンテクストを読み取るか、または提供するかを表します。通常、上位のコンポーネントで [`SomeContext.Provider`](#provider) を使用してコンテクストの値を指定し、下位のコンポーネントで [`useContext(SomeContext)`](/reference/react/useContext) を呼び出してコンテクストを読み取ります。コンテクストオブジェクトにはいくつかのプロパティがあります:
4242

43-
* `SomeContext.Provider` lets you provide the context value to components.
44-
* `SomeContext.Consumer` is an alternative and rarely used way to read the context value.
43+
* `SomeContext.Provider` では、コンポーネントにコンテクストの値を提供できます。
44+
* `SomeContext.Consumer` は、コンテクストの値を読み取るための方法ですが、あまり使用されません。
4545

4646
---
4747

4848
### `SomeContext.Provider` {/*provider*/}
4949

50-
Wrap your components into a context provider to specify the value of this context for all components inside:
50+
コンポーネントをコンテクストプロバイダでラップすると、内部のコンポーネントに対してこのコンテクストの値を指定できます。
5151

5252
```js
5353
function App() {
@@ -61,15 +61,15 @@ function App() {
6161
}
6262
```
6363

64-
#### Props {/*provider-props*/}
64+
#### props {/*provider-props*/}
6565

66-
* `value`: The value that you want to pass to all the components reading this context inside this provider, no matter how deep. The context value can be of any type. A component calling [`useContext(SomeContext)`](/reference/react/useContext) inside of the provider receives the `value` of the innermost corresponding context provider above it.
66+
* `value`: このプロバイダの内側(深さに関わらず)にあるコンポーネントがコンテクストを読み取る際に、渡したい値です。コンテクストの値は任意の型にすることができます。プロバイダ内で [`useContext(SomeContext)`](/reference/react/useContext) を呼び出しているコンポーネントは、それより上位かつ最も内側にある対応するコンテクストプロバイダの `value` を受け取ります。
6767

6868
---
6969

7070
### `SomeContext.Consumer` {/*consumer*/}
7171

72-
Before `useContext` existed, there was an older way to read context:
72+
`useContext` が存在する前には、コンテクストを読み取る古い方法がありました:
7373

7474
```js
7575
function Button() {
@@ -84,7 +84,7 @@ function Button() {
8484
}
8585
```
8686

87-
Although this older way still works, but **newly written code should read context with [`useContext()`](/reference/react/useContext) instead:**
87+
この古い方法はまだ動作しますが、**新しく書かれたコードは [`useContext()`](/reference/react/useContext) を使ってコンテクストを読み取るべきです:**
8888

8989
```js
9090
function Button() {
@@ -94,19 +94,19 @@ function Button() {
9494
}
9595
```
9696

97-
#### Props {/*consumer-props*/}
97+
#### props {/*consumer-props*/}
9898

99-
* `children`: A function. React will call the function you pass with the current context value determined by the same algorithm as [`useContext()`](/reference/react/useContext) does, and render the result you return from this function. React will also re-run this function and update the UI whenever the context from the parent components changes.
99+
* `children`: 関数です。React は、[`useContext()`](/reference/react/useContext) と同じアルゴリズムによって定まる現在のコンテクスト値で関数を呼び出し、その関数から返される結果をレンダーします。親コンポーネントからのコンテクストが変更されると、React はこの関数を再実行し、UI を更新します。
100100

101101
---
102102

103-
## Usage {/*usage*/}
103+
## 使用法 {/*usage*/}
104104

105-
### Creating context {/*creating-context*/}
105+
### コンテクストの作成 {/*creating-context*/}
106106

107-
Context lets components [pass information deep down](/learn/passing-data-deeply-with-context) without explicitly passing props.
107+
コンテクストを利用することで、明示的に props を渡さずに、コンポーネントに[深くまで情報を渡す](/learn/passing-data-deeply-with-context)ことができます。
108108

109-
Call `createContext` outside any components to create one or more contexts.
109+
コンポーネントの外部で `createContext` を呼び出して、コンテクストを 1 つまたは複数個作成します。
110110

111111
```js [[1, 3, "ThemeContext"], [1, 4, "AuthContext"], [3, 3, "'light'"], [3, 4, "null"]]
112112
import { createContext } from 'react';
@@ -115,7 +115,7 @@ const ThemeContext = createContext('light');
115115
const AuthContext = createContext(null);
116116
```
117117

118-
`createContext` returns a <CodeStep step={1}>context object</CodeStep>. Components can read context by passing it to [`useContext()`](/reference/react/useContext):
118+
`createContext` <CodeStep step={1}>コンテクストオブジェクト</CodeStep>を返します。それを [`useContext()`](/reference/react/useContext) に渡すことで、コンポーネントからコンテクストを読み取ることができます:
119119

120120
```js [[1, 2, "ThemeContext"], [1, 7, "AuthContext"]]
121121
function Button() {
@@ -129,9 +129,9 @@ function Profile() {
129129
}
130130
```
131131

132-
By default, the values they receive will be the <CodeStep step={3}>default values</CodeStep> you have specified when creating the contexts. However, by itself this isn't useful because the default values never change.
132+
デフォルトでは、コンポーネントが受け取る値は、コンテクストを作成するときに指定した<CodeStep step={3}>デフォルトの値</CodeStep>になります。しかし、デフォルトの値は決して変わらないため、これ自体では役に立ちませんね。
133133

134-
Context is useful because you can **provide other, dynamic values from your components:**
134+
コンテクストが便利なのは、**コンポーネントから動的な値を提供できる**からです:
135135

136136
```js {8-9,11-12}
137137
function App() {
@@ -150,15 +150,15 @@ function App() {
150150
}
151151
```
152152

153-
Now the `Page` component and any components inside it, no matter how deep, will "see" the passed context values. If the passed context values change, React will re-render the components reading the context as well.
153+
これで、`Page` コンポーネントとその内部のすべてのコンポーネントは、どんなに深くても、渡されたコンテクストの値を「見る」ことができます。渡されたコンテクストの値が変更されると、React はコンテクストを読み取るコンポーネントを再レンダーします。
154154

155-
[Read more about reading and providing context and see examples.](/reference/react/useContext)
155+
[コンテクストの読み取りと提供、例についてさらに読む](/reference/react/useContext)
156156

157157
---
158158

159-
### Importing and exporting context from a file {/*importing-and-exporting-context-from-a-file*/}
159+
### ファイルからのコンテクストのインポートとエクスポート {/*importing-and-exporting-context-from-a-file*/}
160160

161-
Often, components in different files will need access to the same context. This is why it's common to declare contexts in a separate file. Then you can use the [`export` statement](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export) to make context available for other files:
161+
異なるファイルにあるコンポーネントが同じコンテクストにアクセスする必要があることがよくあります。そのため、一般的には別ファイルでコンテクストを宣言します。他のファイルでコンテクストを利用できるようにするために、[`export` ](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export)を使用できます:
162162

163163
```js {4-5}
164164
// Contexts.js
@@ -168,7 +168,7 @@ export const ThemeContext = createContext('light');
168168
export const AuthContext = createContext(null);
169169
```
170170

171-
Components declared in other files can then use the [`import`](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/import) statement to read or provide this context:
171+
他のファイルで宣言されたコンポーネントは、[`import`](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/import) 文を使用して、このコンテクストを読み取ったり、提供したりすることができます:
172172

173173
```js {2}
174174
// Button.js
@@ -196,22 +196,22 @@ function App() {
196196
}
197197
```
198198

199-
This works similar to [importing and exporting components.](/learn/importing-and-exporting-components)
199+
これは[コンポーネントのインポートとエクスポート](/learn/importing-and-exporting-components)と同様に動作します。
200200

201201
---
202202

203-
## Troubleshooting {/*troubleshooting*/}
203+
## トラブルシューティング {/*troubleshooting*/}
204204

205-
### I can't find a way to change the context value {/*i-cant-find-a-way-to-change-the-context-value*/}
205+
### コンテクストの値を変更する方法が見つからない {/*i-cant-find-a-way-to-change-the-context-value*/}
206206

207207

208-
Code like this specifies the *default* context value:
208+
このようなコードは*デフォルト*のコンテクストの値を指定します:
209209

210210
```js
211211
const ThemeContext = createContext('light');
212212
```
213213

214-
This value never changes. React only uses this value as a fallback if it can't find a matching provider above.
214+
この値は決して変わりません。React は、対応するプロバイダを上位のコンポーネントで見つけられない場合にのみ、この値をフォールバックとして使用します。
215215

216-
To make context change over time, [add state and wrap components in a context provider.](/reference/react/useContext#updating-data-passed-via-context)
216+
コンテクストを時間の経過とともに変化させるには、[state を追加し、コンポーネントをコンテクストプロバイダでラップ](/reference/react/useContext#updating-data-passed-via-context)します。
217217

0 commit comments

Comments
 (0)