Diff
checker
文本
文本
图像
文档
Excel
文件夹
Legal
Enterprise
桌面版
定价
登录
下载 Diffchecker 桌面版
比较文本
查找两个文本文件之间的差异
工具
历史
实时编辑器
隐藏空白更改
折叠未更改行
关闭换行
视图
拆分
统一
比对精度
智能
单词
字符
文本样式
更改外观
语法高亮
选择语法
忽略
文本转换
转到第一个差异
编辑输入
Diffchecker Desktop
运行Diffchecker最安全的方式。获取Diffchecker桌面应用:您的差异永远不会离开您的电脑!
获取桌面版
Shared Theme Context - 2
创建于
3个月前
差异永不过期
清除
导出
分享
解释
4 删除
行
总计
删除
字符
总计
删除
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
22 行
全部复制
42 添加
行
总计
添加
字符
总计
添加
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
57 行
全部复制
复制
已复制
复制
已复制
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";
复制
已复制
复制
已复制
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";
});
});
复制
已复制
复制
已复制
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]);
复制
已复制
复制
已复制
const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light"));
const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light"));
复制
已复制
复制
已复制
const value = useMemo(() => ({ theme, setTheme, toggle }), [theme]);
复制
已复制
复制
已复制
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;
}
}
已保存差异
原始文本
打开文件
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 }; }
更改后文本
打开文件
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; }
查找差异