reactjs - 类型 '{ children: Element; }' 不可分配给类型 'IntrinsicAttributes & ReactNode'

标签 reactjs typescript next.js

这里出现这个错误:

Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes & ReactNode'.

export const withAppProvider = (Component: AppComponent) => {
  return function WrapperComponent(props: any) {
    return (
      <AppProvider> // <-- here
        <Component {...props} />
      </AppProvider>
    );
  };
};

AppProvider 声明可能有问题?

export const AppProvider = (children: ReactNode) => {
  const { width, height } = useWindowSize();
  const isClient = useClient();
  const isMobile = isClient ? width < 1200 : false;

  return (
    <AppContext.Provider
      value={{
        isClient,
        isMobile,
      }}
    >
      {children}
    </AppContext.Provider>
  );
};

通过 export const AppProvider = (children: React.ReactNode) => {

得到这个
Type error: Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes & ReactNode'.
  Type '{ children: Element; }' is missing the following properties from type 'ReactPortal': key, type, props

最佳答案

如果将其定义为 children 属性,问题就会消失。

interface IProps {
  children: React.ReactNode;
}
export const AppProvider = ({children}: IProps ) => {}

关于reactjs - 类型 '{ children: Element; }' 不可分配给类型 'IntrinsicAttributes & ReactNode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73417226/

相关文章:

javascript - 如何使用 ReactJS 在复选框更改时更改状态值

javascript - 如何设置 VS Code 来创建 TypeScript 定义文件

javascript - 如何翻译excel PRODUCT公式

azure - 使用 Next-Auth 身份验证从 Azure Active Directory 获取配置文件图像

node.js - 如何在 Next.js 中设置没有自定义服务器的 X-Frame-Options header ?

javascript - React - 使用 reduxForm 的两个语句之间的差异

javascript - ReactJS 即时搜索输入

javascript - 我可以使用 TypeScript 代替 JavaScript 来制作网页吗?

javascript - 如何在 Next.js 组件中使用 SVG 图像?

reactjs - 使用 redux saga 获取数据