Reactjs 打字稿传递道具给孩子们
我对TypeScript很陌生,我正在尝试向Reactjs 打字稿传递道具给孩子们,reactjs,typescript,Reactjs,Typescript,我对TypeScript很陌生,我正在尝试向孩子们发送一些道具,通常我会这样做,-也许这不是最好的方式 app.js import React form 'react' import Component from './component' const App = () => { const myfunc1 = () => 'some function 1' const myfunc2 = () => 'some function 2' const myfunc
孩子们发送一些道具,通常我会这样做,-也许这不是最好的方式
app.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}
/>
)
}
导入反应表单'React'
从“./Component”导入组件
常量应用=()=>{
常量myfunc1=()=>“某些函数1”
常量myfunc2=()=>“某些函数2”
常量myfunc3=()=>“某些函数3”
const var1='some variable 1'
const var2='some variable 2'
返回(
)
}
component.js
import React from 'react'
const Component = ({
myfunc1,
myfunc2,
myfunc3,
var1,
var2,
}) => (
<>
// Use the props on the component
</>
)
export default Component
从“React”导入React
常数分量=({
myfunc1,
myfunc2,
myfunc3,
var1,
var2,
}) => (
//使用组件上的道具
)
导出默认组件
但是当使用TypeScript时,它要求在组件的所有道具上声明类型
是否有更简单的方法传递道具
,这样我就不必在所有道具
上声明类型?从“react”导入{FunctionComponent};
import { FunctionComponent } from 'react';
type Props = {
// types...
}
const Component: FunctionComponent<Props> = ({
myfunc1,
myfunc2,
myfunc3,
var1,
var2,
}) => (
<>
</>
)
类型道具={
//类型。。。
}
常量组件:FunctionComponent=({
myfunc1,
myfunc2,
myfunc3,
var1,
var2,
}) => (
)
从“react”导入{FunctionComponent};
类型道具={
//类型。。。
}
常量组件:FunctionComponent=({
myfunc1,
myfunc2,
myfunc3,
var1,
var2,
}) => (
)
谢谢,但这需要我声明道具的所有类型,如果是函数,它会是什么?是的。TS的职业危害。你必须输入那些东西!使用Function
type您也可以这样做:const Component=(props:props)=>()
,但是TS不知道诸如子项之类的道具。这样,你的道具
扩展了标准的React道具类型如果你的问题是关于通常键入所有道具,那么是的。最好声明所有类型!这就是在React中使用TS的吸引力所在。你说你是新手,给点时间。在任何真正的项目上,这都是非常值得的努力和冗长!谢谢,但这需要我声明道具的所有类型,如果是函数,它会是什么?是的。TS的职业危害。你必须输入那些东西!使用Function
type您也可以这样做:const Component=(props:props)=>()
,但是TS不知道诸如子项之类的道具。这样,你的道具
扩展了标准的React道具类型如果你的问题是关于通常键入所有道具,那么是的。最好声明所有类型!这就是在React中使用TS的吸引力所在。你说你是新手,给点时间。在任何真正的项目上,这都是非常值得的努力和冗长!