[英]Pass a TypeScript Type as a prop to a React component?
Is it possible to pass a TypeScript type as a prop to a React Component?是否可以将 TypeScript 类型作为道具传递给 React 组件?
export type ActivitiesType = {
RUN: "RUN";
WALK: "REST";
ROUNDS: "ROUNDS";
};
<MyComponent activity={ActivitiesType.RUN} />
Then in MyComponent:然后在 MyComponent 中:
const MyComponent = ({ activity }) => {
if(activity === ActivitiesType.RUN) {
// Do something
}
}
Ritaj was right, Enums can do this: Ritaj 是对的,Enums 可以这样做:
enum ActivitiesType {
RUN: "RUN";
WALK: "REST";
ROUNDS: "ROUNDS";
}
type Props = {
type: ActivitiesType;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.