有没有办法检查观察者(组件)的 propTypes
Is there a way to check propTypes for observer(Component)
例如,在这种情况下,如果我将 ObservedA
传递给 B
作为 Comp
:
const ObservedA = observer(class A extends React.Component {});
function B({Comp}) {
return <Comp />;
}
B.propTypes = {
Comp: PropTypes.node // Warning!
};
出现警告:
Warning: Failed prop type: Invalid prop Comp
supplied to B
, expected a ReactNode.
有没有办法检查 propTypes
是否为 observer(Component)
?
我找不到检查 observer(Component)
的正确方法,但我知道 babel 可以将 class
转换为 function() {}
。所以我只是通过 func
检查了道具,例如:
B.propTypes = {
Comp: PropTypes.func
};
我认为这是解决方案之一,但不合适。
如果你想检查 Comp
属性是否是可以渲染的类型,你可以使用 PropTypes.elementType
开头 prop-types 15.7.0
:
B.propTypes = {
Comp: PropTypes.elementType
};
它将对所有可呈现类型都有效,包括例如上下文提供者。
例如,在这种情况下,如果我将 ObservedA
传递给 B
作为 Comp
:
const ObservedA = observer(class A extends React.Component {});
function B({Comp}) {
return <Comp />;
}
B.propTypes = {
Comp: PropTypes.node // Warning!
};
出现警告:
Warning: Failed prop type: Invalid prop
Comp
supplied toB
, expected a ReactNode.
有没有办法检查 propTypes
是否为 observer(Component)
?
我找不到检查 observer(Component)
的正确方法,但我知道 babel 可以将 class
转换为 function() {}
。所以我只是通过 func
检查了道具,例如:
B.propTypes = {
Comp: PropTypes.func
};
我认为这是解决方案之一,但不合适。
如果你想检查 Comp
属性是否是可以渲染的类型,你可以使用 PropTypes.elementType
开头 prop-types 15.7.0
:
B.propTypes = {
Comp: PropTypes.elementType
};
它将对所有可呈现类型都有效,包括例如上下文提供者。