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:37] – [Utiliser les actions] paolo | front:stockage_de_donnees [Date inconnue] (Version actuelle) – supprimée - modification externe (Date inconnue) 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | # Stockage de données | ||
| - | |||
| - | Documentation [IndexedDB](https:// | ||
| - | Documentation [localStorage](https:// | ||
| - | Documentation [sessionStorage](https:// | ||
| - | Documentation [React-redux](https:// | ||
| - | Documentation [Redux Toolkit](https:// | ||
| - | |||
| - | Redux est une lib de gestion d' | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | |||
| - | |||
| - | |||
| - | ### 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**: | ||
| - | |||
| - | * On crée des slices (" | ||
| - | |||
| - | ``` | ||
| - | import { createSlice } from " | ||
| - | |||
| - | const initialState = { | ||
| - | data: JSON.parse(sessionStorage.getItem(" | ||
| - | }; | ||
| - | |||
| - | const sessionSlice = createSlice({ | ||
| - | name: " | ||
| - | initialState, | ||
| - | reducers: { | ||
| - | setSession(state, | ||
| - | const session = action.payload; | ||
| - | | ||
| - | state.data = session; | ||
| - | sessionStorage.setItem(" | ||
| - | }, | ||
| - | unsetSession(state) { | ||
| - | state.data = null; | ||
| - | sessionStorage.removeItem(" | ||
| - | } | ||
| - | }, | ||
| - | }); | ||
| - | |||
| - | export default sessionSlice.reducer; | ||
| - | export const { setSession, unsetSession } = sessionSlice.actions; | ||
| - | |||
| - | ``` | ||
| - | * On combine les reducers issus des slices via '' | ||
| - | |||
| - | ``` | ||
| - | import sessionReducer from " | ||
| - | |||
| - | import { combineReducers } from " | ||
| - | import { configureStore } from " | ||
| - | |||
| - | const rootReducer = combineReducers({ | ||
| - | session: sessionReducer | ||
| - | }); | ||
| - | |||
| - | export const redux = configureStore({ reducer: rootReducer }); | ||
| - | ``` | ||
| - | |||
| - | On peut aussi exporter toutes les actions du slice dans l' | ||
| - | |||
| - | ``` | ||
| - | export * from " | ||
| - | ``` | ||
| - | |||
| - | - On passe ce store à ''< | ||
| - | |||
| - | ``` | ||
| - | import { Provider } from " | ||
| - | import { redux } from " | ||
| - | |||
| - | export const ReduxProvider = (props) => { | ||
| - | const { children } = props; | ||
| - | |||
| - | return ( | ||
| - | < | ||
| - | {children} | ||
| - | </ | ||
| - | ); | ||
| - | }; | ||
| - | ``` | ||
| - | ``` | ||
| - | import { Router } from " | ||
| - | import { ReduxProvider } from " | ||
| - | |||
| - | export const App = () => { | ||
| - | return ( | ||
| - | < | ||
| - | <Router /> | ||
| - | </ | ||
| - | ); | ||
| - | }; | ||
| - | ``` | ||
| - | Maintenant l' | ||
| - | ### Utiliser l' | ||
| - | |||
| - | Pour récupérer l' | ||
| - | |||
| - | Maintenant qu'on a les moyens de savoir si une session est ouverte, reprenons nos composants '' | ||
| - | |||
| - | ``` | ||
| - | import { Outlet, Navigate } from " | ||
| - | import { useSelector } from " | ||
| - | |||
| - | export const PublicRoutes = () => { | ||
| - | const session = useSelector(state => state.session.data); | ||
| - | | ||
| - | return session ? < | ||
| - | }; | ||
| - | ``` | ||
| - | ``` | ||
| - | import { Outlet, Navigate } from " | ||
| - | import { useSelector } from " | ||
| - | |||
| - | export const PrivateRoutes = () => { | ||
| - | const session = useSelector(state => state.session.data); | ||
| - | | ||
| - | return session ? <Outlet /> : < | ||
| - | }; | ||
| - | ``` | ||
| - | |||
| - | Vous êtes maintenant bloqué sur la page **Login**. Mais tout va bien ! Nous allons essayer de nous connecter. | ||
| - | ### Utiliser les actions | ||
| - | |||
| - | Reprenons à présent à présent notre requête de connexion et essayons de modifier l' | ||
| - | |||
| - | ``` | ||
| - | import { Input } from " | ||
| - | import { useDispatch } from " | ||
| - | import { setSession } from " | ||
| - | |||
| - | export const Login = () => { | ||
| - | 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 => setSession(json.data)) | ||
| - | .catch(error => console.error(error)); | ||
| - | }; | ||
| - | | ||
| - | return ( | ||
| - | // page login | ||
| - | ); | ||
| - | }; | ||
| - | ``` | ||
| - | |||
| - | |||