![](/img/trans.png)
[英]What are the right types for a useContext with TypeScript/ReactJS?
[英]TypeScript : What is the right way to define types
我對 typescript 類型的分配有點困惑。 在我的示例中,我想知道如何定義不會是“任何類型”的道具類型,以及如何定義標題的類型、handleOnPress、svg SideMenuItem的參數SideMenuItem
const SideMenu = (props: any) => {
const SideMenuItem = ({ title, handleOnPress, svg }) => (
<TouchableOpacity style={styles.sideMenuItem} onPress={handleOnPress}>
<View style={styles.syncView}>
{svg}
<Text style={styles.sideMenuItemText}>{title}</Text>
</View>
</TouchableOpacity>
);
在界面中為您的組件定義道具類型
interface SideMenuItemProps {
title: string;
handleOnPress: () => void;
svg: ReactNode;
}
然后在組件定義中定義prop類型
const SideMenuItem = ({ title, handleOnPress, svg }: SideMenuItemProps) => (
//...
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.