2015-04-28 45 views
0

当你创建一个组件A其中包含一个组件B通过添加一些逻辑?你是否应该提醒所需propTypes B in A包装组件是否应提醒包装组件的propType?

例如之实践:

const HorizontalGauge = React.createClass({ 

    propTypes: { 
    //Should I remind required propTypes of GenericHorizontalGauge ? 
    showPercentage: PropTypes.bool, 
    }, 

    _formatStackValuePercentage() { 
    ... 
    } 

    render() { 
    let { showPercentage, ...otherProps } = this.props; 


    return (
     <GenericHorizontalGauge 
     formatValue={showPercentage && this._formatValuePercentage} 
     {...otherProps} 
     /> 
    ); 
} 

回答

1

你是问,如果因为每个道具从HorizontalGauge传递到GenericHorizontalGaugeGenericHorizontalGauge应具有相同的propTypesHorizontalGauge

在这种情况下,答案是肯定的,他们应该。为避免重复,您可以在一个地方定义道具并重复使用。事情是这样的:

const HorizontalGauge = React.createClass({ 
    propTypes: GenericHorizontalGauge.propTypes, 
    /* Other methods here */ 
}); 
+0

爷那是什么我也这样想在这种情况下Horizo​​ntalGauge propTypes应该像 'propTypes:{... GenericHorizo​​ntalGauge.propTypes,showPercentage:PropTypes.bool}' – Nicolas