简体   繁体   English

TypeScript 检入 React 功能组件

[英]TypeScript checking in React Functional components

I am starting out with TypeScript on a React app (next.js) and I'm not sure how to type React components.我从 React 应用程序 (next.js) 上的 TypeScript 开始,我不确定如何键入 React 组件。

I have a component like...我有一个像...

type Props = {
  children: JSX.Element;
};

export default function SiteLayout({ children }: Props): React.FC {
return (
  <div>
    {children}
  </div>
)

But this gives me the following error:但这给了我以下错误:

var children: JSX.Element var children:JSX.Element
Type 'Element' is not assignable to type 'FC<{}>'.类型“元素”不可分配给类型“FC<{}>”。
Type 'Element' provides no match for the signature '(props: { children?: ReactNode; }, context?: any): ReactElement<any, any>'.ts(2322)类型 'Element' 不匹配签名 '(props: { children?: ReactNode; }, context?: any): ReactElement<any, any>'.ts(2322)

I also tried to get it working with simply using JSX.Element :我还尝试使用JSX.Element让它工作:

type Props = {
  children: JSX.Element;
};

export default function SiteLayout({ children }: Props): JSX.Element {

return (
  <div>
    {children}
  </div>
)

But this gives the following error in child components.但这会在子组件中产生以下错误。

(alias) function SiteLayout({ children }: Props): JSX.Element import SiteLayout (别名) function SiteLayout({ children }: Props): JSX.Element import SiteLayout
This JSX tag's 'children' prop expects a single child of type 'Element', but multiple children were provided.ts(2746)这个 JSX 标签的“children”属性需要一个“Element”类型的子元素,但提供了多个子元素。ts(2746)

The above component is being consumed in by a Next.js page component like:上述组件被 Next.js页面组件消耗,例如:

import Head from 'next/head';
import SiteLayout, { siteTitle } from '../../components/SiteLayout';

export default function myPage() {
  const pageTitle = 'My Page';


  return (
    <SiteLayout>
      <Head>
        <title>{siteTitle + ' - ' + pageTitle}</title>
      </Head>
      <div>
        content...
      </div>
    </SiteLayout>
  );
}

What is the correct approach?什么是正确的方法? I'm confused when JSX.Element should be used and when React.FC should be.我很困惑何时应该使用JSX.Element以及何时应该使用React.FC

The return type of the function is not a component, the function itself is component. function 的返回类型不是组件,function 本身就是组件。 That's why instead of这就是为什么而不是

function SiteLayout({ children }: Props): React.FC { ...

It should be:它应该是:

const SiteLayout: React.FC<Props> = ({ children }: Props) => {
    return (
      <div>
        {children}
      </div>
    );
}

Regarding the type of children prop, if there's no restriction on kind of children (string, boolean, element etc,) better to use ReactNode :关于children道具的类型,如果对子项的种类(字符串、boolean、元素等)没有限制,最好使用ReactNode

type Props = {
    children: React.ReactNode
}

Here's how it is defined. 这是它的定义方式。

Your function is returning a React.FC type, which shouldnt be the case.您的 function 正在返回 React.FC 类型,这不应该是这种情况。 The return type of function components is ReactNode, plus children are implicitly provided by the React.FC typing. function 组件的返回类型是 ReactNode,而子组件由 React.FC 类型隐式提供。 If you explicitly type them, you can omit the React.FC and write:如果你明确输入它们,你可以省略 React.FC 并写:

const SiteLayout = ({children}: Props) => {
  return (
    <div>
      {children}
    </div>
  )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿 - 反应0.14无状态功能组件 - Typescript - React 0.14 stateless functional components React TypeScript:功能组件中 UseRef 的替代方案 - React TypeScript: Alternatives to UseRef in functional components 如何使用 react 和 typescript 在反应功能组件中渲染子级? - How to render children within react functional components using react and typescript? 使用 TypeScript、功能组件和钩子在 React 上下文提供程序中设置状态 - Setting state in React context provider with TypeScript, functional components and hooks React,Typescript中无状态功能组件内的事件处理 - Event handling inside stateless functional components in React, Typescript 与 Typescript 功能组件反应 - 如何从父级调用子方法 - React with Typescript functional components - how to call child method from parent 带有 typescript 的路由器道具和自定义道具为功能组件反应路由器 dom - router props and custom props with typescript react router dom for functional components 使用 Typescript generics 进行带有 eslint props 验证的 React 功能组件 - Using Typescript generics for React Functional Components with eslint props validation 是否可以使用 typescript 声明 generics 反应功能组件? - Is it possible to declare generics react functional components using typescript? 如何使用 TypeScript 为无状态的功能性 React 组件指定(可选)默认道具? - How to specify (optional) default props with TypeScript for stateless, functional React components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM