reactjs - 将函数和其他属性传递给 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} />
}
类似(或接近)的事情可能吗?
解决方案
推荐阅读
- json - 无法在 Swift 中解析 JSON
- javascript - 需要在子图选择上显示 X 轴标签
- mysql - 带附加表的三向内连接
- android - Android Q:file.mkdirs() 返回 false
- javascript - 日期之间的差异,四舍五入到最接近的分钟
- r - R中函数中的特殊字符
- python - 有人可以解释一下这些括号内所有代码的含义是什么:RegexpTokenizer(r'\w+|$[0-9]+|\S+')?
- php - 未定义的属性:stdClass::$images
- php - 如何解决将刀片语法转换为 php 的问题?刀片中的 if 语句到 php
- arangodb - 我无法在 ArangoDB 的 FOR 循环中多次更新同一个文档