在基于类的组件中,我可以轻松地编写如下代码:
import * as React from 'react';
import { render } from 'react-dom';
interface IProps<T> {
collapsed: boolean;
listOfData: T[];
displayData: (data: T, index: number) => React.ReactNode;
}
class CollapsableDataList<T> extends React.Component<IProps<T>> {
render () {
if (!this.props.collapsed) {
return <span>total: {this.props.listOfData.length}</span>
} else {
return (
<>
{
this.props.listOfData.map(this.props.displayData)
}
</>
)
}
}
}
render(
<CollapsableDataList
collapsed={false}
listOfData={[{a: 1, b: 2}, {a: 3, b: 4}]}
displayData={(data, index) => (<span key={index}>{data.a + data.b}</span>)}
/>,
document.getElementById('root'),
)
实际上,这个CollapsableDataList
组件应该是功能性组件,因为它是无状态的,但是我无法找出如何编写功能性组件并在props中使用泛型的方法,
答案 0 :(得分:8)
您不能创建带有类型注释的功能组件并使之通用。因此,由于T
未定义,因此您无法在变量级别上定义它,因此将无法使用:
const CollapsableDataList : React.FunctionComponent<IProps<T>> = p => { /*...*/ }
但是,您可以跳过类型注释,使函数通用并显式键入props
。
import * as React from 'react';
import { render } from 'react-dom';
interface IProps<T> {
collapsed: boolean;
listOfData: T[];
displayData: (data: T, index: number) => React.ReactNode;
}
const CollapsableDataList = <T extends object>(props: IProps<T> & { children?: ReactNode }) => {
if (!props.collapsed) {
return <span>total: {props.listOfData.length}</span>
} else {
return (
<>
{
props.listOfData.map(props.displayData)
}
</>
)
}
}
render(
<CollapsableDataList
collapsed={false}
listOfData={[{a: 1, b: 2}, {a: 3, c: 4}]}
displayData={(data, index) => (<span key={index}>{data.a + (data.b || 0)}</span>)}
/>,
document.getElementById('root'),
)
答案 1 :(得分:4)
type Props<T> = {
active: T;
list: T[];
onChange: (tab: T) => void;
};
export const Tabs = <T,>({ active, list, onChange }: Props<T>): JSX.Element => {
return (
<>
{list.map((tab) => (
<Button onClick={() => onChange(tab)} active={tab === active}>
{tab}
</Button>
))}
</>
);
};
答案 2 :(得分:3)
类型React.FC
本质上是这样的:
<P = {}>(props: PropsWithChildren<P>, context?: any) => ReactElement | null
所以代替这个(不允许):
const Example: React.FC<Props<P>> = (props) => {
// return a React element or null
}
您可以使用此:
const Example = <P = {}>(props: PropsWithChildren<Props<P>>): ReactElement | null => {
// return a React element or null
}
答案 3 :(得分:1)
在讨论功能组件之前,我假设原始代码示例缺少JSX组件中的泛型,因为我看不到它传递给IProps
接口。即:
interface Ab {
a: number;
b: number;
}
...
// note passing the type <Ab> which will eventually make it to your IProps<T> interface and cascade the type for listOfData
return (
<CollapsableDataList<Ab>
collapsed={false}
listOfData={[{a: 1, b: 2}, {a: 3, c: 4}]}
...
/>
)
好了,现在您实际上可以拥有一个带有通用道具的功能组件。
尽管使用了“赋值”和“箭头”功能,但是对于“通用”情况却没有用:
// using this syntax there is no way to pass generic props
const CollapsableDataList: React.FC<IProps> = ({ collapsed, listOfData }) => {
// logic etc.
return (
// JSX output
);
}
让我们将变量赋值重写为旧的function
:
// we are now able to to write our function component with generics
function CollapsableDataList<T>({ collapsed, listOfData }: IProps<T> & { children?: React.ReactNode }): React.ReactElement {
// logic etc.
return (
// JSX output
);
}
如果组件不使用children prop,则不一定需要children
解决方法,但是我添加了它来突出显示必须手动重新键入它的事实,因为React.FC
为我们做了以前。