web:framework:react

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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:21] – [Exemple classe TypeScript] jcheronweb:framework:react [2024/04/10 09:40] (Version actuelle) jcheron
Ligne 96: Ligne 96:
 ==== Exemple classe TypeScript ==== ==== Exemple classe TypeScript ====
  
-<sxh typescript>+<sxh javascript;title:components/hello.tsx>
 import React from "react"; import React from "react";
  
Ligne 112: Ligne 112:
           <h1>{this.state.msg}</h1>           <h1>{this.state.msg}</h1>
           <input value={this.state.msg} onChange={this.handleChange}/>           <input value={this.state.msg} onChange={this.handleChange}/>
-        <nowiki></></nowiki>+        < />
     );     );
   }   }
 } }
 </sxh> </sxh>
 +
 +==== Exemple fonction JavaScript ====
 +
 +<sxh javascript;title:components/hola.js>
 +import {useState} from "react";
 +
 +export default function Hola(props){
 +    const [message, setMessage] = useState(props.message);
 +    const handleChange = (e) => {
 +        setMessage(e.target.value);
 +    }
 +    return (
 +      <>
 +        <h1>{message}</h1>
 +        <input type="text" value={message} onChange={handleChange}/>
 +      < />
 +    );
 +}
 +</sxh>
 +
 +==== 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: 'POST',
 +            body: JSON.stringify(body),
 +            ...options
 +        });
 +        return await response.json();
 +    }
 +    static async put(url, body, options) {
 +        const response = await fetch(url, {
 +            method: 'PUT',
 +            body: JSON.stringify(body),
 +            ...options
 +        });
 +        return await response.json();
 +    }
 +    static async delete(url, options) {
 +        const response = await fetch(url, {
 +            method: 'DELETE',
 +            ...options
 +        });
 +        return await response.json();
 +    }
 +}
 +</sxh>
 +
 +==== Contexte ====
 +
 +<sxh ts>
 +'use client';
 +import React, {createContext, ReactNode, useContext, useState} from 'react';
 +
 +
 +interface DemoContextType {
 +    data: number;
 +    setData: (d:number)=>void;
 +}
 +
 +const DemoContext = createContext<DemoContextType | undefined>(undefined);
 +
 +export const demoProvider: React.FC<{ children: ReactNode }> = ({children}) => {
 +    const [data, setData] = useState<number>(0);
 +
 +    return (
 +        <DemoContext.Provider value={{data, setData}}>
 +            {children}
 +        </DemoContext.Provider>
 +    );
 +};
 +
 +export const useDemo = (): DemoContextType => {
 +    const context = useContext(demoContext);
 +    if (!context) {
 +        throw new Error('usedemo must be used within a DemoProvider');
 +    }
 +    return context;
 +};
 +</sxh>
 +
 +===== Composants =====
 +  * [[https://react-hook-form.com/|React-hook-form]]
 +  * [[https://tanstack.com/query/v3/|React-query]]
  
  
  
  • web/framework/react.1679008878.txt.gz
  • Dernière modification : il y a 2 ans
  • de jcheron