Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| front:stockage_de_donnees [2025/09/26 12:20] – paolo | front:stockage_de_donnees [2025/09/29 16:42] (Version actuelle) – [Utiliser les actions] paolo | ||
|---|---|---|---|
| Ligne 11: | Ligne 11: | ||
| * '' | * '' | ||
| * '' | * '' | ||
| - | |||
| ### Monter son Redux store | ### Monter son Redux store | ||
| - | Le store Redux est l’objet qui contient l’état global. Pour le mettre en place, il faut utiliser **Redux Toolkit**: | + | Le store **Redux** est l’objet qui contient l’état global. Pour le mettre en place, il faut utiliser **Redux Toolkit**: |
| - | * On crée des slices (" | + | * Nous créons |
| ``` | ``` | ||
| + | // src/ | ||
| + | |||
| import { createSlice } from " | import { createSlice } from " | ||
| Ligne 48: | Ligne 49: | ||
| ``` | ``` | ||
| - | * On combine | + | * Nous combinons |
| ``` | ``` | ||
| + | // src/ | ||
| + | |||
| import sessionReducer from " | import sessionReducer from " | ||
| Ligne 60: | Ligne 64: | ||
| }); | }); | ||
| - | export const redux = configureStore({ reducer: rootReducer }); | + | export const reduxStore |
| ``` | ``` | ||
| - | On peut aussi exporter toutes les actions du slice dans l' | + | Les actions du slice peuvent aussi être exportées |
| ``` | ``` | ||
| Ligne 69: | Ligne 73: | ||
| ``` | ``` | ||
| - | - On passe ce store à ''< | + | - Ce store est passé au ''< |
| ``` | ``` | ||
| + | // src/ | ||
| + | |||
| import { Provider } from " | import { Provider } from " | ||
| - | import { redux } from " | + | import { reduxStore |
| export const ReduxProvider = (props) => { | export const ReduxProvider = (props) => { | ||
| Ligne 79: | Ligne 85: | ||
| return ( | return ( | ||
| - | < | + | < |
| {children} | {children} | ||
| </ | </ | ||
| Ligne 86: | Ligne 92: | ||
| ``` | ``` | ||
| ``` | ``` | ||
| + | // src/App.jsx | ||
| + | |||
| import { Router } from " | import { Router } from " | ||
| import { ReduxProvider } from " | import { ReduxProvider } from " | ||
| Ligne 97: | Ligne 105: | ||
| }; | }; | ||
| ``` | ``` | ||
| - | Maintenant | + | Désormais |
| + | . | ||
| ### Utiliser l' | ### Utiliser l' | ||
| - | Pour récupérer l' | + | Pour récupérer l' |
| - | Maintenant | + | Maintenant |
| ``` | ``` | ||
| + | // src/ | ||
| + | |||
| import { Outlet, Navigate } from " | import { Outlet, Navigate } from " | ||
| import { useSelector } from " | import { useSelector } from " | ||
| Ligne 115: | Ligne 126: | ||
| ``` | ``` | ||
| ``` | ``` | ||
| + | // src/ | ||
| + | |||
| import { Outlet, Navigate } from " | import { Outlet, Navigate } from " | ||
| import { useSelector } from " | import { useSelector } from " | ||
| Ligne 125: | Ligne 138: | ||
| ``` | ``` | ||
| - | Vous êtes maintenant | + | Nous sommes |
| + | . | ||
| ### Utiliser les actions | ### Utiliser les actions | ||
| - | Reprenons à présent à présent notre requête de connexion et essayons de modifier l' | + | Reprenons à présent à présent notre requête de connexion et essayons de modifier l' |
| + | ``` | ||
| + | // src/ | ||
| + | import { Input } from " | ||
| + | import { useDispatch } from " | ||
| + | import { setSession } from " | ||
| + | import { API_URL } from " | ||
| + | export const Login = () => { | ||
| + | const dispatch = useDispatch(); | ||
| + | | ||
| + | const handleFormOnSubmit = (e) => { | ||
| + | e.preventDefault() // empêche le comportement par défaut du formulaire | ||
| + | | ||
| + | const data = new FormData(e.target) | ||
| + | const values = Object.fromEntries(data.entries()); | ||
| + | | ||
| + | const request = { | ||
| + | method: " | ||
| + | body: values, | ||
| + | headers: { | ||
| + | Accept: " | ||
| + | " | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | fetch(`${API_URL}/ | ||
| + | .then(response => response.json()) | ||
| + | .then(json => dispatch(setSession(json.data))) | ||
| + | .catch(error => console.error(error)); | ||
| + | }; | ||
| + | | ||
| + | return ( | ||
| + | // page login | ||
| + | ); | ||
| + | }; | ||
| + | ``` | ||
| + | Et voilà vous maîtrisez la base de **Redux**. Il ne vous reste plus qu'à rajouter une fonction de déconnexion (dans votre page **Home** par exemple) et vous avez une application avec un système d' | ||