包装器组件是否应该提醒包装组件的 propTypes?
Should wrappers component remind propTypes of wrapped components?
当您创建组件 A 并通过添加一些逻辑来包装组件 B 时?您是否应该提醒 A 中 B 所需的 propTypes?
实际例子:
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}
/>
);
}
你是在问 GenericHorizontalGauge
是否应该与 HorizontalGauge
具有相同的 propTypes
因为每个 prop 都是从 HorizontalGauge
传递到 GenericHorizontalGauge
的?
在那种情况下,答案是肯定的,他们应该这样做。为避免重复,您可以在一个地方定义道具并重复使用。像这样:
const HorizontalGauge = React.createClass({
propTypes: GenericHorizontalGauge.propTypes,
/* Other methods here */
});
当您创建组件 A 并通过添加一些逻辑来包装组件 B 时?您是否应该提醒 A 中 B 所需的 propTypes?
实际例子:
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}
/>
);
}
你是在问 GenericHorizontalGauge
是否应该与 HorizontalGauge
具有相同的 propTypes
因为每个 prop 都是从 HorizontalGauge
传递到 GenericHorizontalGauge
的?
在那种情况下,答案是肯定的,他们应该这样做。为避免重复,您可以在一个地方定义道具并重复使用。像这样:
const HorizontalGauge = React.createClass({
propTypes: GenericHorizontalGauge.propTypes,
/* Other methods here */
});