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:33] – [Exemple classe TypeScript] jcheron | web:framework:react [2024/04/10 09:40] (Version actuelle) – jcheron | ||
---|---|---|---|
Ligne 96: | Ligne 96: | ||
==== Exemple classe TypeScript ==== | ==== Exemple classe TypeScript ==== | ||
- | <sxh javascript;> | + | <sxh javascript;title: |
import React from " | import React from " | ||
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 137: | Ligne 137: | ||
</ | </ | ||
+ | ==== 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; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | ===== Composants ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||