【问题标题】:What would be the return type for React function [duplicate]React 函数的返回类型是什么 [重复]
【发布时间】:2019-12-23 17:12:10
【问题描述】:

我是新的打字稿和 React。

我正在创建一个容器

import React from 'react'

interface containerProps {
  heading: string,
  para: string
}

const container = (props: containerProps) => {
    return (
        <>
        </>
    )
}

但这给了我一个 JSX 错误,说 type expected.

react JSX 返回类型应该是什么?

【问题讨论】:

标签: reactjs typescript


【解决方案1】:

假设您使用的是 React 16.8,提供类型的正确方法是使用 React.FC 或 React.FunctionComponent。然后,我们可以提供组件的 props(ContainerProps) 作为泛型参数的一部分。

import * as React from 'react';

interface ContainerProps {
  heading: string,
  para: string
}

const container: React.FC<ContainerProps> = (props: ContainerProps) => {
  return (
    <>
    </>
  )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2017-09-25
    • 2018-08-06
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多