Skip to content

Translate 'createContext' #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 39 additions & 38 deletions src/content/reference/react/createContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: createContext

<Intro>

`createContext` lets you create a [context](/learn/passing-data-deeply-with-context) that components can provide or read.
`createContext` rajapinnan avulla voit luoda [kontekstin](/learn/passing-data-deeply-with-context), jota komponentit voivat tarjota tai lukea.

```js
const SomeContext = createContext(defaultValue)
Expand All @@ -16,38 +16,38 @@ const SomeContext = createContext(defaultValue)

---

## Reference {/*reference*/}
## Referenssi {/*reference*/}

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

Call `createContext` outside of any components to create a context.
Kutsu `createContext` -funktiota komponenttien ulkopuolella luodaksesi kontekstin.

```js
import { createContext } from 'react';

const ThemeContext = createContext('light');
```

[See more examples below.](#usage)
[Katso lisää esimerkkejä alapuolelta.](#käyttö)

#### Parameters {/*parameters*/}
#### Parametrit {/*parameters*/}

* `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.
* `defaultValue`: Arvo, jonka haluat kontekstilla olevan, kun puun yläpuolella olevista komponenteista ei löydy vastaavaa kontekstin tarjoajaa. Jos sinulla ei ole mitään merkityksellistä oletusarvoa, määritä `null`. Oletusarvo on tarkoitettu "viimeisenä keinona" vara-arvona. Se on staattinen eikä muutu ajan myötä.

#### Returns {/*returns*/}
#### Palautukset {/*returns*/}

`createContext` returns a context object.
`createContext` palauttaa kontekstiolion.

**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:
**Kontekstiolio itsessään ei sisällä mitään tietoa.** Se edustaa _minkä_ kontekstin muita komponentteja lukee tai tarjoaa. Tyypillisesti käytät [`SomeContext.Provider`](#provider) ylemmissä komponenteissa määrittääksesi kontekstin arvon ja kutsut [`useContext(SomeContext)`](/reference/react/useContext) -komponenttia alempana lukeaksesi sen. Kontekstioliossa on muutama ominaisuus:

* `SomeContext.Provider` lets you provide the context value to components.
* `SomeContext.Consumer` is an alternative and rarely used way to read the context value.
* `SomeContext.Provider` avulla voit tarjota kontekstin arvon komponenteille.
* `SomeContext.Consumer` on vaihtoehtoinen ja harvoin käytetty tapa lukea kontekstin arvo.

---

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

Wrap your components into a context provider to specify the value of this context for all components inside:
Kääri komponenttisi kontekstin tarjoajaan määrittääksesi tämän kontekstin arvon kaikille sisäpuolella oleville komponenteille:

```js
function App() {
Expand All @@ -61,19 +61,19 @@ function App() {
}
```

#### Props {/*provider-props*/}
#### Propsit {/*provider-props*/}

* `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.
* `value`: Arvo, jonka haluat välittää kaikille tämän tarjoajan sisällä oleville kontekstin lukeville komponenteille, riippumatta siitä, kuinka syvällä ne ovat. Kontekstin arvo voi olla mitä tahansa tyyppiä. Komponentti, joka kutsuu [`useContext(SomeContext)`](/reference/react/useContext) -Hookkia tarjoajan sisällä, saa `value`:n vastaavasta kontekstin tarjoajasta, joka on sen yläpuolella.

---

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

Before `useContext` existed, there was an older way to read context:
Ennen kuin `useContext` oli olemassa, oli vanhempi tapa lukea konteksti:

```js
function Button() {
// 🟡 Legacy way (not recommended)
// 🟡 Vanha tapa (ei suositella)
return (
<ThemeContext.Consumer>
{theme => (
Expand All @@ -84,29 +84,29 @@ function Button() {
}
```

Although this older way still works, but **newly written code should read context with [`useContext()`](/reference/react/useContext) instead:**
Vaikka tämä vanhempi tapa silti toimii, **uuden koodin tulisi lukea konteksti [`useContext()`](/reference/react/useContext) -hookilla:**

```js
function Button() {
// ✅ Recommended way
// ✅ Suositeltu tapa
const theme = useContext(ThemeContext);
return <button className={theme} />;
}
```

#### Props {/*consumer-props*/}
#### Propsit {/*consumer-props*/}

* `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.
* `children`: Funktio. React kutsuu funktiota, johon välität nykyisen kontekstin arvon, joka on määritetty samalla algoritmilla kuin [`useContext()`](/reference/react/useContext) tekee, ja renderöi tuloksen, jonka palautat tästä funktiosta. React myös uudelleen suorittaa tämän funktion ja päivittää käyttöliittymän aina kun konteksti ylemmistä komponenteista muuttuu.

---

## Usage {/*usage*/}
## Käyttö {/*usage*/}

### Creating context {/*creating-context*/}
### Kontekstin luominen {/*creating-context*/}

Context lets components [pass information deep down](/learn/passing-data-deeply-with-context) without explicitly passing props.
Contekstin avulla komponentit voivat [välittää tietoa syvälle](/learn/passing-data-deeply-with-context) ilman, että ne välittävät eksplisiittisesti propseja.

Call `createContext` outside any components to create one or more contexts.
Kutsu `createContext` -funktiota komponenttien ulkopuolella luodaksesi yhden tai useamman kontekstin.

```js [[1, 3, "ThemeContext"], [1, 4, "AuthContext"], [3, 3, "'light'"], [3, 4, "null"]]
import { createContext } from 'react';
Expand All @@ -115,7 +115,8 @@ const ThemeContext = createContext('light');
const AuthContext = createContext(null);
```

`createContext` returns a <CodeStep step={1}>context object</CodeStep>. Components can read context by passing it to [`useContext()`](/reference/react/useContext):
`createContext` palauttaa <CodeStep step={1}>kontekstiolion</CodeStep>.
Komponentit voivat lukea kontekstin välittämällä sen [`useContext()`](/reference/react/useContext) -hookille:

```js [[1, 2, "ThemeContext"], [1, 7, "AuthContext"]]
function Button() {
Expand All @@ -129,9 +130,9 @@ function Profile() {
}
```

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.
Oletuksena arvot, jotka ne saavat, ovat <CodeStep step={3}>oletusarvoja</CodeStep>, jotka olet määrittänyt luodessasi kontekstit. Kuitenkin, itsessään tämä ei ole hyödyllistä, koska oletusarvot eivät koskaan muutu.

Context is useful because you can **provide other, dynamic values from your components:**
Kontekstit ovat hyödyllisiä, koska voit **tarjota muita, dynaamisia arvoja komponenteistasi:**

```js {8-9,11-12}
function App() {
Expand All @@ -150,15 +151,15 @@ function App() {
}
```

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.
Nyt `Page` komponentti ja kaikki sen sisällä olevat komponentit, riippumatta siitä kuinka syvällä, "näkevät" välitetyt kontekstin arvot. Jos välitetyt kontekstin arvot muuttuvat, React uudelleen renderöi myös kontekstin lukevat komponentit.

[Read more about reading and providing context and see examples.](/reference/react/useContext)
[Lue lisää kontekstin lukemisesta sekä tarjoamisesta ja katso esimerkkejä.](/reference/react/useContext)

---

### Importing and exporting context from a file {/*importing-and-exporting-context-from-a-file*/}
### Kontekstin tuominen ja vieminen tiedostosta {/*importing-and-exporting-context-from-a-file*/}

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:
Usein, eri tiedostoissa olevat komponentit tarvitsevat pääsyn samaan kontekstiin. Tämän vuoksi on yleistä määrittää kontekstit erillisessä tiedostossa. Voit sitten käyttää [`export` -lauseketta](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export) tehdäksesi kontekstin saataville muille tiedostoille:

```js {4-5}
// Contexts.js
Expand All @@ -168,7 +169,7 @@ export const ThemeContext = createContext('light');
export const AuthContext = createContext(null);
```

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:
Muissa tiedostoissa määritellyt komponentit voivat sitten käyttää [`import`](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/import) -lauseketta lukeakseen tai tarjotakseen tämän kontekstin:

```js {2}
// Button.js
Expand Down Expand Up @@ -196,22 +197,22 @@ function App() {
}
```

This works similar to [importing and exporting components.](/learn/importing-and-exporting-components)
Tämä toimii samalla tavalla kuin [komponenttien tuominen ja vieminen.](/learn/importing-and-exporting-components)

---

## Troubleshooting {/*troubleshooting*/}
## Vianmääritys {/*troubleshooting*/}

### I can't find a way to change the context value {/*i-cant-find-a-way-to-change-the-context-value*/}
### En löydä tapaa muuttaa kontekstin arvoa {/*i-cant-find-a-way-to-change-the-context-value*/}


Code like this specifies the *default* context value:
Seuraavanlainen koodi määrittää *oletusarvon* kontekstin arvolle:

```js
const ThemeContext = createContext('light');
```

This value never changes. React only uses this value as a fallback if it can't find a matching provider above.
Tämä arvo ei koskaan muutu. React käyttää tätä arvoa vain varmuusarvona, jos se ei löydä vastaavaa tarjoajaa yläpuolelta.

To make context change over time, [add state and wrap components in a context provider.](/reference/react/useContext#updating-data-passed-via-context)
Jotta konteksti muuttuisi ajan myötä, [lisää tila ja kääri komponentit kontekstin tarjoajaan.](/reference/react/useContext#updating-data-passed-via-context)