You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/6/en/part6.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,9 @@ So far, we have placed the application's state and state logic directly inside R
10
10
11
11
We'll learn about the lightweight version of Redux directly supported by React, namely the React context and useRedux hook, as well as the React Query library that simplifies the server state management.
12
12
13
-
<i>Part updated 30th Jan 2023</i>
14
-
- <i>A new section on React Query, useReducer and React context replaced the section on Redux connect</i>
Copy file name to clipboardExpand all lines: src/content/6/en/part6d.md
+21-18Lines changed: 21 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,20 +50,20 @@ The initial code is on GitHub in the repository [https://github.com/fullstack-hy
50
50
51
51
### Managing data on the server with the React Query library
52
52
53
-
We shall now use the [React Query](https://react-query-v3.tanstack.com/) library to store and manage data retrieved from the server.
53
+
We shall now use the [React Query](https://tanstack.com/query/latest) library to store and manage data retrieved from the server. The latest version of the library is also called TanStack Query, but we stick to the familiar name.
54
54
55
55
Install the library with the command
56
56
57
57
```bash
58
-
npm install react-query
58
+
npm install @tanstack/react-query
59
59
```
60
60
61
-
A few additions to the file <i>index.js</i> are needed to pass the library functions to the entire application:
61
+
A few additions to the file <i>main.jsx</i> are needed to pass the library functions to the entire application:
To create a new note, a [mutation](https://tanstack.com/query/latest/docs/react/guides/mutations) is defined using the function [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutation):
Let us now create a context in the application that stores the state management of the counter.
534
537
535
-
The context is created with React's hook [createContext](https://beta.reactjs.org/reference/react/createContext). Let's create a context in the file <i>CounterContext.js</i>:
538
+
The context is created with React's hook [createContext](https://beta.reactjs.org/reference/react/createContext). Let's create a context in the file <i>CounterContext.jsx</i>:
536
539
537
540
```js
538
541
import { createContext } from'react'
@@ -594,7 +597,7 @@ The current code for the application is in [GitHub](https://github.com/fullstack
594
597
595
598
### Defining the counter context in a separate file
596
599
597
-
Our application has an annoying feature, that the functionality of the counter state management is partly defined in the <i>App</i> component. Now let's move everything related to the counter to <i>CounterContext.js</i>:
600
+
Our application has an annoying feature, that the functionality of the counter state management is partly defined in the <i>App</i> component. Now let's move everything related to the counter to <i>CounterContext.jsx</i>:
598
601
599
602
```js
600
603
import { createContext, useReducer } from'react'
@@ -629,7 +632,7 @@ export default CounterContext
629
632
630
633
The file now exports, in addition to the <i>CounterContext</i> object corresponding to the context, the <i>CounterContextProvider</i> component, which is practically a context provider whose value is a counter and a dispatcher used for its state management.
631
634
632
-
Let's enable the context provider by making a change in <i>index.js</i>:
635
+
Let's enable the context provider by making a change in <i>main.jsx</i>:
Copy file name to clipboardExpand all lines: src/content/6/fi/osa6.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,8 @@ Olemme toistaiseksi sijoittaneet ohjelman tilan ja siitä huolehtivan logiikan s
10
10
11
11
Tututustumme Reactin suoraan tukemaan Reduxin kevytversioon, eli Reactin kontekstiin ja useRedux-hookiin sekä palvelimen tilan hallintaa helpottavaan React Query ‑kirjastoon.
12
12
13
-
<i>Osa päivitetty 1.2.2023</i>
14
-
- <i>Uusi luku, aiheenaan React Query, useReducer ja React context korvasi Redux connect ‑luvun</i>
Copy file name to clipboardExpand all lines: src/content/6/fi/osa6d.md
+26-22Lines changed: 26 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,18 +50,19 @@ Alkuvaiheen koodi on GitHubissa repositorion [https://github.com/fullstack-hy202
50
50
51
51
### Palvelimella olevan datan hallinta React Query ‑kirjaston avulla
52
52
53
-
Hyödynnämme nyt [React Query](https://react-query-v3.tanstack.com/) ‑kirjastoa palvelimelta haettavan datan säilyttämiseen ja hallinnointiin. Asennetaan kirjasto komennolla
53
+
Hyödynnämme nyt [React Query](https://tanstack.com/query/latest) ‑kirjastoa palvelimelta haettavan datan säilyttämiseen ja hallinnointiin. Kirjaston uusimmasta versiosta käytetään myös nimitystä TanStack Query mutta pitäydymme vanhassa tutussa nimessä.
54
+
55
+
Asennetaan kirjasto komennolla
54
56
55
57
```bash
56
-
npm install react-query
58
+
npm install @tanstack/react-query
57
59
```
58
60
59
-
Tiedostoon <i>index.js</i> tarvitaan muutama lisäys, jotta kirjaston funktiot saadaan välitettyä koko sovelluksen käyttöön:
61
+
Tiedostoon <i>main.jsx</i> tarvitaan muutama lisäys, jotta kirjaston funktiot saadaan välitettyä koko sovelluksen käyttöön:
Datan hakeminen palvelimelta tapahtuu edelleen tuttuun tapaan Axiosin <i>get</i>-metodilla. Axiosin metodikutsu on kuitenkin nyt kääritty [useQuery](https://react-query-v3.tanstack.com/reference/useQuery)-funktiolla muodostetuksi [kyselyksi](https://react-query-v3.tanstack.com/guides/queries). Funktiokutsun ensimmäisenä parametrina on merkkijono <i>notes</i> joka toimii [avaimena](https://react-query-v3.tanstack.com/guides/query-keys) määriteltyyn kyselyyn, eli muistiinpanojen listaan.
110
+
Datan hakeminen palvelimelta tapahtuu edelleen tuttuun tapaan Axiosin <i>get</i>-metodilla. Axiosin metodikutsu on kuitenkin nyt kääritty [useQuery](https://tanstack.com/query/latest/docs/react/reference/useQuery)-funktiolla muodostetuksi [kyselyksi](https://tanstack.com/query/latest/docs/react/guides/queries). Funktiokutsun ensimmäisenä parametrina on merkkijono <i>notes</i> joka toimii [avaimena](https://tanstack.com/query/latest/docs/react/guides/query-keys) määriteltyyn kyselyyn, eli muistiinpanojen listaan.
110
111
111
112
Funktion <i>useQuery</i> paluuarvo on olio, joka kertoo kyselyn tilan. Konsoliin tehty tulostus havainnollistaa tilannetta:
Uuden muistiinpanon luomista varten määritellään [mutaatio](https://react-query-v3.tanstack.com/guides/mutations) funktion [useMutation](https://react-query-v3.tanstack.com/reference/useMutation) avulla:
194
+
Uuden muistiinpanon luomista varten määritellään [mutaatio](https://tanstack.com/query/latest/docs/react/guides/mutations) funktion [useMutation](https://tanstack.com/query/latest/docs/react/reference/useMutatio) avulla:
Ratkaisumme on hyvä. Paitsi se ei toimi. Uusi muistiinpano kyllä tallettuu palvelimelle, mutta se ei päivity näytölle.
205
209
206
210
Jotta saamme renderöityä myös uuden muistiinpanon, meidän on kerrottava React Querylle, että kyselyn, jonka avaimena on merkkijono <i>notes</i> vanha tulos tulee mitätöidä eli
@@ -596,7 +600,7 @@ Sovelluksen tämänhetkinen koodi on GitHubissa repositorion [https://github.com
596
600
597
601
### Laskurikontekstin määrittely omassa tiedostossa
598
602
599
-
Sovelluksessamme on vielä sellainen ikävä piirre, että laskurin tilanhallinnan toiminnallisuus on määritelty osin komponentissa <i>App</i>. Siirretään nyt kaikki laskuriin liittyvä tiedostoon <i>CounterContext.js</i>:
603
+
Sovelluksessamme on vielä sellainen ikävä piirre, että laskurin tilanhallinnan toiminnallisuus on määritelty osin komponentissa <i>App</i>. Siirretään nyt kaikki laskuriin liittyvä tiedostoon <i>CounterContext.jsx</i>:
600
604
601
605
```js
602
606
import { createContext, useReducer } from'react'
@@ -631,7 +635,7 @@ export default CounterContext
631
635
632
636
Tiedosto exporttaa nyt kontekstia vastaavan olion <i>CounterContext</i> lisäksi komponentin <i>CounterContextProvider</i> joka on käytännössä kontekstin tarjoaja (context provider), jonka arvona on laskuri ja sen tilanhallintaan käytettävä dispatcheri.
633
637
634
-
Otetaan kontekstin tarjoaja käyttöön tiedostossa <i>index.js</i>
638
+
Otetaan kontekstin tarjoaja käyttöön tiedostossa <i>main.jsx</i>
0 commit comments