Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
web:framework:react [2023/03/17 00:26] – [Exemple classe TypeScript] jcheron | web:framework:react [2025/08/12 02:35] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 96: | Ligne 96: | ||
==== Exemple classe TypeScript ==== | ==== Exemple classe TypeScript ==== | ||
- | <sxh javascript;> | + | <sxh javascript;title: |
import React from " | import React from " | ||
Ligne 109: | Ligne 109: | ||
render() { | render() { | ||
return ( | return ( | ||
- | <div> | + | <> |
< | < | ||
<input value={this.state.msg} onChange={this.handleChange}/> | <input value={this.state.msg} onChange={this.handleChange}/> | ||
- | </div> | + | < /> |
); | ); | ||
} | } | ||
Ligne 120: | Ligne 120: | ||
==== Exemple fonction JavaScript ==== | ==== Exemple fonction JavaScript ==== | ||
- | <sxh javascript;> | + | <sxh javascript;title: |
import {useState} from " | import {useState} from " | ||
- | export default function | + | export default function |
const [message, setMessage] = useState(props.message); | const [message, setMessage] = useState(props.message); | ||
const handleChange = (e) => { | const handleChange = (e) => { | ||
Ligne 132: | Ligne 132: | ||
< | < | ||
<input type=" | <input type=" | ||
- | </> | + | < /> |
); | ); | ||
} | } | ||
</ | </ | ||
+ | ==== Http Wrapper ==== | ||
+ | |||
+ | <sxh javascript> | ||
+ | // This is a wrapper for the fetch API | ||
+ | export default class AppHttp { | ||
+ | static async get(url, options) { | ||
+ | const response = await fetch(url, options); | ||
+ | return await response.json(); | ||
+ | } | ||
+ | static async post(url, body, options) { | ||
+ | const response = await fetch(url, { | ||
+ | method: ' | ||
+ | body: JSON.stringify(body), | ||
+ | ...options | ||
+ | }); | ||
+ | return await response.json(); | ||
+ | } | ||
+ | static async put(url, body, options) { | ||
+ | const response = await fetch(url, { | ||
+ | method: ' | ||
+ | body: JSON.stringify(body), | ||
+ | ...options | ||
+ | }); | ||
+ | return await response.json(); | ||
+ | } | ||
+ | static async delete(url, options) { | ||
+ | const response = await fetch(url, { | ||
+ | method: ' | ||
+ | ...options | ||
+ | }); | ||
+ | return await response.json(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Contexte ==== | ||
+ | |||
+ | <sxh ts> | ||
+ | 'use client'; | ||
+ | import React, {createContext, | ||
+ | |||
+ | |||
+ | interface DemoContextType { | ||
+ | data: number; | ||
+ | setData: (d: | ||
+ | } | ||
+ | |||
+ | const DemoContext = createContext< | ||
+ | |||
+ | export const demoProvider: | ||
+ | const [data, setData] = useState< | ||
+ | |||
+ | return ( | ||
+ | < | ||
+ | {children} | ||
+ | </ | ||
+ | ); | ||
+ | }; | ||
+ | |||
+ | export const useDemo = (): DemoContextType => { | ||
+ | const context = useContext(demoContext); | ||
+ | if (!context) { | ||
+ | throw new Error(' | ||
+ | } | ||
+ | return context; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Hooks React ===== | ||
+ | Les hooks React sont des fonctions introduites à partir de React 16.8 permettant d' | ||
+ | |||
+ | ==== Hooks de base ==== | ||
+ | |||
+ | ===== useState ===== | ||
+ | |||
+ | Permet de gérer un état local dans un composant fonctionnel. | ||
+ | |||
+ | <sxh js> | ||
+ | import { useState } from " | ||
+ | |||
+ | function Counter() { | ||
+ | const [count, setCount] = useState(0); | ||
+ | |||
+ | return ( | ||
+ | <button onClick={() => setCount(count + 1)}> | ||
+ | ); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== useEffect ===== | ||
+ | |||
+ | |||
+ | Permet d' | ||
+ | Exemples : | ||
+ | * appels API, | ||
+ | * écouteurs d’événements, | ||
+ | * manipulation du DOM... | ||
+ | |||
+ | <sxh js> | ||
+ | import { useState, useEffect } from " | ||
+ | |||
+ | function Timer() { | ||
+ | const [seconds, setSeconds] = useState(0); | ||
+ | |||
+ | useEffect(() => { | ||
+ | const interval = setInterval(() => { | ||
+ | setSeconds(s => s + 1); | ||
+ | }, 1000); | ||
+ | |||
+ | return () => clearInterval(interval); | ||
+ | }, []); // Dépendances vides = effet exécuté une seule fois au montage | ||
+ | |||
+ | return < | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== useContext ===== | ||
+ | |||
+ | Permet d' | ||
+ | |||
+ | <sxh js> | ||
+ | import { useContext, createContext } from " | ||
+ | |||
+ | const ThemeContext = createContext(" | ||
+ | |||
+ | function ThemedButton() { | ||
+ | const theme = useContext(ThemeContext); | ||
+ | |||
+ | return <button style={{ background: theme === " | ||
+ | Thème : {theme} | ||
+ | </ | ||
+ | } | ||
+ | </ | ||
+ | ===== Composants ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||