2017-10-10 98 views
0

验证除了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

+0

你想要在'icon'属性中验证什么? – juliobetta

+0

“我用PropTypes.element,它抱怨说”它说了什么? – Aaronius

+0

@juliobetta我想验证传入的图标组件本身,通常它是无状态的,这实际上只是一个函数。理由是我可能会通过它其他类型的组件,并寻找的东西,而不仅仅是验证功能 –

回答

0

没有更好的办法。使用PropTypes.func

+0

我有时不会传递一个无状态的组件,我需要一些通用的东西 –

相关问题