reactjs 类型脚本错误:类型"ReactNode"上不存在属性"children"

qij5mzcb  于 2023-02-04  发布在  React
关注(0)|答案(9)|浏览(271)
export const PageViewTracker = ({ children }: ReactNode): ReactElement => {

    usePageView();

    return children;
};
    • 问题:**

此函数返回错误 * 属性'children'在类型'ReactNode'上不存在 *

    • 我的解决方法:**

我尝试了几种类型,但只有 * any * 工作,这不是我想要的。通常我使用reactnode作为儿童 prop ,它工作得很好。在这种情况下,typescript似乎有问题。

nc1teljy

nc1teljy1#

React〉=18

在React 18中,FunctionalComponent接口已更改为:

interface FunctionComponent<P = {}> {
    (props: P, context?: any): ReactElement<any, any> | null;
    propTypes?: WeakValidationMap<P> | undefined;
    contextTypes?: ValidationMap<any> | undefined;
    defaultProps?: Partial<P> | undefined;
    displayName?: string | undefined;
}

请注意,在React 18之后,PropsWithChildren类型从功能组件的props类型中省略,这意味着您必须自己包含children属性:

interface Props {
  children: React.ReactNode;
}

export const PageViewTracker: React.FC<Props> = ({ children }) => {
}

他们删除隐式children属性的原因可以在here中找到(React 18的Type definition release notes的source)。
PropsWithChildren类型在React的类型中仍然可用,因此如果您仍然希望像React 18之前的版本一样选择性地将children作为 prop ,您仍然可以执行以下操作:

import { PropsWithChildren } from 'react';

interface Props {
  foo: string;
}

export const PageViewTracker: React.FC<PropsWithChildren<Props>> = ({ children, foo }) => {
}

PropsWithChildren的类型定义为:

type PropsWithChildren<P = unknown> = P & { children?: ReactNode | undefined };

React〈=17

出现该错误的原因是因为您将'ReactNode'接口提供给了一个对象({}: Type)。children本身的类型为ReactNode:
type PropsWithChildren<P> = P & { children?: ReactNode };
您应该给予PageViewTracker指定FunctionComponent(或其别名FC)类型。

export const PageViewTracker: React.FC = ({ children }) => {
   ...
}

其具有如下接口:

interface FunctionComponent<P = {}> {
    (props: PropsWithChildren<P>, context?: any): ReactElement | null;
    propTypes?: WeakValidationMap<P>;
    contextTypes?: ValidationMap<any>;
    defaultProps?: Partial<P>;
    displayName?: string;
}

因此,默认情况下,它接受类型为“ReactNode”的children属性。

mwngjboj

mwngjboj2#

重要提示:在react 18.0.0之后,它们会让您在每个FC接口中包含子接口。

interface MyButtonProps {
  color: string;
  children?: React.ReactNode;
}

然后你可以像老版本一样传递 prop 。

const Button:React.FC<MyButtonProps> = (props) => {
    //use children with {props.children} just like before  
}

来自文档

4ioopgfo

4ioopgfo3#

对于“@类型/React”:“17.0.43”,或小于18

import { FunctionComponent } from "react";

const BaseLayout: FunctionComponent = ({ children }) => {
  return (
    <>      
    </>
  );
};

export default BaseLayout;

对于“@类型/React”〉18

interface BaseLayoutProps {
  children?: ReactNode;
}

const BaseLayout: FunctionComponent<BaseLayoutProps> = ({ children }) => {
  return (
    <>          
    </>
  );
};
3mpgtkmj

3mpgtkmj4#

您可以简单地这样写:

export const PageViewTracker = ({ children }: {children: ReactNode}): ReactElement => {

    usePageView();

    return children;
};

请注意,我将({ children }: ReactNode)更改为({ children }: {children: ReactNode})

2cmtqfgy

2cmtqfgy5#

import React from 'react';

interface Props {
  children?: React.ReactNode;
}

export const MenuLateral: React.FC<Props> = ({ children }) => {
  return (
    <>
      
      {children}
    </>
  );
};
wtlkbnrh

wtlkbnrh6#

正如Dan在他的一条推文中提到的,"如果你在升级@types/react到18.0.0时遇到了关于children prop 的TypeScript错误,修复程序如下所示。你需要在props类型中将它们声明为一个常规 prop ,类型为React.ReactNode。"
React 18
你可以在这里查看他的推文:https://twitter.com/dan_abramov/status/1512833611401150474
或者stackoverflow线程在这里:React18个TypeScript子级FC

x7rlezfr

x7rlezfr7#

你可以试试这个:

import { ReactNode } from "react";

export const PageViewTracker= ( { children }: {children: ReactNode} ) => {

        usePageView();

        return children;
    };
9njqaruj

9njqaruj8#

可以在泛型类型参数中注解它的类型:

import React, {ReactNode} from "react";

const Hello: React.FC<{ children: ReactNode }> = ({children}) => {
    return <React.Fragment>
        Hello {children}
    </React.Fragment>;
}
tkqqtvp1

tkqqtvp19#

import Navbar from "../Navbar/Navbar";

interface Layout {
  children?: ReactNode;
}

const Layout: React.FC<Layout> = ({ children }) => {
  return (
    <>
      <Navbar />
      <main>{children}</main>
    </>
  );
};
export default Layout;

相关问题