reactjs - TypeScript 将道具传递给孩子
问题描述
我是 TypeScript 的新手,我正在尝试发送一些props
到 a children
,通常我会这样做,-也许这不是最好的方法。
应用程序.js
import React form 'react'
import Component from './component'
const App = () => {
const myfunc1 = () => 'some function 1'
const myfunc2 = () => 'some function 2'
const myfunc3 = () => 'some function 3'
const var1 = 'some variable 1'
const var2 = 'some variable 2'
return (
<Component
myfunc1={myfunc1}
myfunc2={myfunc2}
myfunc3={myfunc3}
var1={var1}
var2={var2}
/>
)
}
组件.js
import React from 'react'
const Component = ({
myfunc1,
myfunc2,
myfunc3,
var1,
var2,
}) => (
<>
// Use the props on the component
</>
)
export default Component
但是在使用 TypeScript 时,它会要求在 all props
on 上进行类型声明Component
。
有没有更好更简单的传递方式,props
所以我不必在 all 上声明类型props
?
解决方案
import { FunctionComponent } from 'react';
type Props = {
// types...
}
const Component: FunctionComponent<Props> = ({
myfunc1,
myfunc2,
myfunc3,
var1,
var2,
}) => (
<>
</>
)
推荐阅读
- java - 这些代码中的问号是什么意思?
- javascript - 日期作为图表的索引
- python - 如何监控游戏地址值?
- angular - 更改 Angular/Jasmine/Redux 中每个测试的模拟服务返回的 observable 值
- javascript - 未捕获的错误:对象作为 React 子项无效(找到:[object HTMLImageElement])
- java - 在 LWJGL 的 OpenCL 功能中,我从哪里获得“cl_platform_id”?
- javascript - React 路由器和 TypeScript 实现
- java - 将图像动态应用到菜单项
- php - 创建没有数据库表的自定义控制器
- angular - 有没有办法将 {{}} 参数传递给点击内的函数?