Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何将泛型与React.FC一起使用_Reactjs_Typescript_Generics - Fatal编程技术网

Reactjs 如何将泛型与React.FC一起使用

Reactjs 如何将泛型与React.FC一起使用,reactjs,typescript,generics,Reactjs,Typescript,Generics,如何将泛型与React.FC一起使用 代码: 从“React”导入React 导出接口IWaterFallProps{ 类名?:字符串; 样式?:React.csp属性; 子节点?:React.ReactNode; 数据源?:T[]; rowKey?:((项目:T)=>字符串)|字符串; renderItem?:(项目:T,索引:编号)=>React.ReactNode; 标题?:React.ReactNode; 页脚?:React.ReactNode; } //这不是错误 常量瀑布:React

如何将泛型与React.FC一起使用

代码:

从“React”导入React
导出接口IWaterFallProps{
类名?:字符串;
样式?:React.csp属性;
子节点?:React.ReactNode;
数据源?:T[];
rowKey?:((项目:T)=>字符串)|字符串;
renderItem?:(项目:T,索引:编号)=>React.ReactNode;
标题?:React.ReactNode;
页脚?:React.ReactNode;
}
//这不是错误
常量瀑布:React.FC=props=>{
返回(
{props.header}
{props.children}
{props.footer}
)
}
瀑布.defaultProps={
数据源:[],
}
导出默认的React.memo(瀑布式)
'常量瀑布:React.FC=props=>{}'

这个T是不对的,它是如何工作的


链接:

只需使用普通函数声明即可

function WaterFall<T>(props: React.PropsWithChildren<IWaterFallProps<T>>) {
    // your code
}
函数瀑布(道具:React.PropsWithChildren){
//你的代码
}
我已经看到了大量使用“复杂”类型和包装器实现这种行为的技巧和方法,但是当您可以使用如上所示的正常函数声明时,我认为没有必要这样做。

``函数瀑布(props:PropsWithChildren&{defaultProps?:Partial;displayName?:string;},{//您的代码}“如果我希望像上面那样使用默认道具?
function WaterFall<T>(props: React.PropsWithChildren<IWaterFallProps<T>>) {
    // your code
}