我创建了一个名为ThemeContext的文件:

import { createContext, useState } from "react";
import PropTypes from "prop-types";
import { DARK_THEME, LIGHT_THEME } from "../constants/themeConstants";

interface ThemeContextType {
  theme: string;
  toggleTheme: () => void;
}

export const ThemeContext = createContext<ThemeContextType>({
  theme: LIGHT_THEME,
  toggleTheme: () => {}
});

export const ThemeProvider: React.FC<{}> = ({ children }: any) => {
  const [theme, setTheme] = useState<string>(
    window.localStorage.getItem("themeMode") || LIGHT_THEME
  );

  window.localStorage.setItem("themeMode", theme);

  const toggleTheme = () => {
    setTheme((prevTheme) =>
      prevTheme === LIGHT_THEME ? DARK_THEME : LIGHT_THEME
    );
    window.localStorage.setItem("themeMode", theme);
  };

  return (
    <ThemeContext.Provider
      value={{
        theme,
        toggleTheme,
      }}
    >
      {children}
    </ThemeContext.Provider>
  );
};

ThemeProvider.propTypes = {
  children: PropTypes.node.isRequired,
};

这就是我使用主题提供程序的地方:

import "./App.scss";
import { ThemeProvider } from "./context/ThemeContext";

import Admin from "./routes/admin/Admin";
import Global from "./routes/global/Global";

function App() {
  return (
    <div className="App">
      <Global />
      
      <ThemeProvider>
        <Admin></Admin>
     
      </ThemeProvider>
        
     
    </div>
  );
}

export default App;

但我有一个错误,那就是: 类型‘{Child:Element;}’与类型‘IntrinsicAttributes’没有相同的属性.ts(2559) (别名)模块主题提供程序 (别名)const ThemeProvider:React.fc&lt;{}&gt; 导入ThemeProvider

我试图理解这个问题,但似乎ThemeProvider不接受它所包装的任何元素. 我希望有人能帮帮我

推荐答案

您键入了ThemeProvider组件以使其没有props ,但在包装组件时传递子props .any类型与React.FC<{}>中传递的{}类型冲突.

使用React.PropsWithChildren型.

export const ThemeProvider: React.FC<React.PropsWithChildren<{}>> = ({
   children
 }) => {
  ...
};

或者只是

export const ThemeProvider = ({ children }: React.PropsWithChildren<{}>) => {
  ...
};

如果你有其他组件props ,它们会被传到PropsWithChildren.

interface ThemeProviderProps {
  someProp: string;
  someNumber: number;
}

export const ThemeProvider = (
  { children, someProp, someNumber }: React.PropsWithChildren<{ThemeProviderProps}>
) => {
  ...
};

您还需要使用一个初始化器函数来设置初始的theme状态,并使用一个useEffect挂钩来在theme状态值更改时发出更新本地存储的副作用.

示例:

import { PropsWithChildren } from 'react';

export const ThemeProvider = ({ children }: PropsWithChildren<{}>) => {
  const [theme, setTheme] = useState<string>(
    () => window.localStorage.getItem("themeMode") || LIGHT_THEME
  );

  useEffect(() => {
    window.localStorage.setItem("themeMode", theme);
  }, [theme]);

  ...
};

Typescript相关问答推荐

为什么文字必须硬编码才能在TypScript中工作?

调用另一个函数的函数的Typescript类型,参数相同

更新:Typescript实用程序函数合并对象键路径

如果存在ts—mock—imports,我们是否需要typescpt中的IoC容器

如何在排版中正确键入中间件链和控制器链

如何为父类构造函数中的修饰属性赋值?

使用`renderToPipeableStream`时如何正确恢复服务器端Apollo缓存?

无法绑定ngModel.条件ngSwitchCase中的错误

在分配给类型的只读变量中维护const的类型

使用Dockerfile运行Vite React应用程序时访问env变量

Select 下拉菜单的占位符不起作用

Vue3 Uncaught SyntaxError:每当我重新加载页面时,浏览器控制台中会出现无效或意外的令牌

如何在ANGLE中注册自定义验证器

如何过滤文字中的类对象联合类型?

在组件卸载时try 使用useEffect清除状态时发生冲突.react +打字

为什么TypeScrip假定{...省略类型,缺少类型,...选取类型,添加&>}为省略类型,缺少类型?

在REACT查询中获取未定义的isLoading态

使用泛型以自引用方式静态键入记录的键

导航链接不触发自定义挂钩

如何提取具有索引签名的类型中定义的键