首页 > 解决方案 > 将函数和其他属性传递给 React 功能组件

问题描述

我们的团队有一个约定以这种方式将属性传递给功能组件:

type FCProps = {
  aVar: SomeType,
  anotherVar: string
}

const MyFC: React.FC<FCProps> = ({aVar, anotherVar}) => {
    return <SomeComponent aVar={aVar} anotherVar={anotherVar} />
}

在浏览了一些与功能组件相关的TypeScript 文档和 React文档后,我能想到的最好的方法是:

type FCProps = {
  aVar: SomeType,
  anotherVar: string,
  (myBool: boolean): void
}

const MyFC: React.FC<FCProps> = (props: FCProps) => {
    const someValue = props(1)
    return <SomeComponent aVar={props.aVar} anotherVar={props.anotherVar} handlerFunc={props} />
}

我真正想要的是看起来像这样的东西:

type FCProps = {
  aVar: SomeType,
  anotherVar: string,
  (myBool: boolean): void
}

const MyFC: React.FC<FCProps> = ({aVar, anotherVar, myFunc}) => {
    const someValue = props(1)
    return <SomeComponent aVar={aVar} anotherVar={anotherVar} handlerFunc={myFunc} />
}

类似(或接近)的事情可能吗?

标签: reactjsreact-functional-component

解决方案


推荐阅读