Diff
checker
Testo
Testo
Immagini
Documenti
Excel
Cartelle
Legal
Enterprise
Applicazione per desktop
Prezzi
Accedi
Scarica Diffchecker Desktop
Confronta il testo
Trova la differenza tra due file di testo
Strumenti
Cronologia
Editor live
Nascondi spazi bianchi
Comprimi invariate
Senza a capo
Layout
Diviso
Unificato
Livello di dettaglio
Intelligente
Parola
Carattere
Stili testo
Modifica aspetto
Evidenziazione sintassi
Scegli sintassi
Ignora
Trasforma testo
Vai alla prima modifica
Modifica input
Diffchecker Desktop
Il modo più sicuro per usare Diffchecker. Ottieni l'app Diffchecker Desktop: i tuoi diff non lasciano mai il tuo computer!
Ottieni Desktop
Shared Theme Context - 2
Creato
3 mesi fa
Il diff non scade mai
Eliminare
Esporta
Condividere
Spiegare
4 rimozioni
Linee
Totale
Rimosso
Caratteri
Totale
Rimosso
Per continuare a utilizzare questa funzione, aggiorna a
Diff
checker
Pro
Visualizza prezzi
22 linee
Copia tutti
42 aggiunte
Linee
Totale
Aggiunto
Caratteri
Totale
Aggiunto
Per continuare a utilizzare questa funzione, aggiorna a
Diff
checker
Pro
Visualizza prezzi
57 linee
Copia tutti
Copia
Copiato
Copia
Copiato
import {
useEffect,
useState
} from "react";
import {
createElement,
createContext,
type Dispatch,
type PropsWithChildren,
type SetStateAction,
useContext,
useEffect,
useMemo,
useState
,
} from "react";
type Theme = "light" | "dark";
type Theme = "light" | "dark";
Copia
Copiato
Copia
Copiato
export function
useTheme(
) {
interface ThemeContextValue {
theme: Theme;
setTheme: Dispatch<SetStateAction<Theme>>;
toggle: () => void;
}
const ThemeContext = createContext<ThemeContextValue | null>(null);
export function
ThemeProvider({ children }: PropsWithChildren
) {
const [theme, setTheme] = useState<Theme>(() => {
const [theme, setTheme] = useState<Theme>(() => {
if (typeof window !== "undefined") {
if (typeof window !== "undefined") {
return (localStorage.getItem("theme") as Theme) || "light";
return (localStorage.getItem("theme") as Theme) || "light";
}
}
return "light";
return "light";
});
});
Copia
Copiato
Copia
Copiato
useEffect(() => {
useEffect(() => {
const root = document.documentElement;
const root = document.documentElement;
root.classList.toggle("dark", theme === "dark");
root.classList.toggle("dark", theme === "dark");
localStorage.setItem("theme", theme);
localStorage.setItem("theme", theme);
}, [theme]);
}, [theme]);
Copia
Copiato
Copia
Copiato
const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light"));
const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light"));
Copia
Copiato
Copia
Copiato
const value = useMemo(() => ({ theme, setTheme, toggle }), [theme]);
Copia
Copiato
Copia
Copiato
return
{ theme, setTheme, toggle };
return
createElement(ThemeContext.Provider, { value }, children);
}
export function useTheme() {
const context = useContext(ThemeContext);
if (!context) {
throw new Error("useTheme must be used within a ThemeProvider");
}
return context;
}
}
Diff salvati
Testo originale
Apri file
import { useEffect, useState } from "react"; type Theme = "light" | "dark"; export function useTheme() { const [theme, setTheme] = useState<Theme>(() => { if (typeof window !== "undefined") { return (localStorage.getItem("theme") as Theme) || "light"; } return "light"; }); useEffect(() => { const root = document.documentElement; root.classList.toggle("dark", theme === "dark"); localStorage.setItem("theme", theme); }, [theme]); const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light")); return { theme, setTheme, toggle }; }
Testo modificato
Apri file
import { createElement, createContext, type Dispatch, type PropsWithChildren, type SetStateAction, useContext, useEffect, useMemo, useState, } from "react"; type Theme = "light" | "dark"; interface ThemeContextValue { theme: Theme; setTheme: Dispatch<SetStateAction<Theme>>; toggle: () => void; } const ThemeContext = createContext<ThemeContextValue | null>(null); export function ThemeProvider({ children }: PropsWithChildren) { const [theme, setTheme] = useState<Theme>(() => { if (typeof window !== "undefined") { return (localStorage.getItem("theme") as Theme) || "light"; } return "light"; }); useEffect(() => { const root = document.documentElement; root.classList.toggle("dark", theme === "dark"); localStorage.setItem("theme", theme); }, [theme]); const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light")); const value = useMemo(() => ({ theme, setTheme, toggle }), [theme]); return createElement(ThemeContext.Provider, { value }, children); } export function useTheme() { const context = useContext(ThemeContext); if (!context) { throw new Error("useTheme must be used within a ThemeProvider"); } return context; }
Trovare la differenza