强制支柱应为两个功能组件之一
我想强制执行道具应该是两个不同的组件之一,基本上是这样:
MyComponent.propTypes {
propA: PropTypes.oneOfType([
PropTypes.instanceOf(ClassComponentA)
PropTypes.instanceOf(ClassComponentB)
]),
}
仅当classComponenta
和classComponentB
都是类组件时,它才能起作用。当classComponenta
和classComponentB
都是功能组件时,我该如何工作?该解决方案不必使用 proptypes 。
I want to enforce a prop should be one of two different components, basically this:
MyComponent.propTypes {
propA: PropTypes.oneOfType([
PropTypes.instanceOf(ClassComponentA)
PropTypes.instanceOf(ClassComponentB)
]),
}
The problem with this is it only works if ClassComponentA
and ClassComponentB
both are class components. How can I make it work when ClassComponentA
and ClassComponentB
are both functional components? The solution doesn't have to use PropTypes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,功能组件只是接收道具并返回JSX元素的函数。
因此,您可以执行以下操作:
在打字稿中,它可能更简单(在我看来):
编辑:更好的打字稿版本
Well, a functional component is just a function that receives props and returns a JSX Element.
So you could do something like the following:
In typescript, it could be a little more simple (in my opinion):
EDIT: Better typescript version