验证除了Proptypes.func
以外的无状态组件的proptypes的最佳方式是什么?验证作为道具传递的组件的最佳方法
比方说,我有一个NestedDashboardItem
组件将作为道具收到Icon
组件。我不想把它作为一个孩子使用,并且从props.children
开始使用它,因为我有一些使用情况,这会像传递一组嵌套的子链接等那样矫枉过正。
考虑:
<DashboardItem
to="/route"
icon={someIconComponent}
text="sometext"
/>
,并在DashboardItem
定义我想验证该图标托
const DashboardItem = ({ text, icon, to, classes }) => {
const Icon = icon;
return (
<ListItem
button
component={Link}
to={to}
activeClassName="active"
className={classes.root}
>
<ListItemIcon className={cx(classes.dashboardIcon_wrapper)}>
<Icon className={cx(classes.icon)} />
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
);
};
我用PropTypes.element
,它抱怨,所以现在我要问是什么验证icon
支柱的最佳方法,而不是PropTypes.func
?
你想要在'icon'属性中验证什么? – juliobetta
“我用PropTypes.element,它抱怨说”它说了什么? – Aaronius
@juliobetta我想验证传入的图标组件本身,通常它是无状态的,这实际上只是一个函数。理由是我可能会通过它其他类型的组件,并寻找的东西,而不仅仅是验证功能 –