admin管理员组文章数量:1332889
I am trying to give a type to one of my properties 'listOfItems'. I want this property to be an array of either instance of class 'Event' or instance of class 'Venue'. This is how I have implemented it:
MyClass.propTypes = {
...,
listOfItems: PropTypes.arrayOf(PropTypes.oneOfType([
PropTypes.instanceOf(Event),
PropTypes.instanceOf(Venue)
]))
}
However, it doesn't seem to be working. I receive this warning: Failed prop type: Invalid prop listOfItems[0]
of value [object Object]
supplied to MyClass
, expected one of [null,null].
I don't want to use PropTypes.shape to define array elements, as I will get quite an extended list of shape properties. This is what I mean:
MyClass.propTypes = {
...,
listOfItems: PropTypes.arrayOf(PropTypes.shape({
... //list of properties
}))
}
Please share your thought how would you define an array of objects with certain properties without writing each property of an object.
I am trying to give a type to one of my properties 'listOfItems'. I want this property to be an array of either instance of class 'Event' or instance of class 'Venue'. This is how I have implemented it:
MyClass.propTypes = {
...,
listOfItems: PropTypes.arrayOf(PropTypes.oneOfType([
PropTypes.instanceOf(Event),
PropTypes.instanceOf(Venue)
]))
}
However, it doesn't seem to be working. I receive this warning: Failed prop type: Invalid prop listOfItems[0]
of value [object Object]
supplied to MyClass
, expected one of [null,null].
I don't want to use PropTypes.shape to define array elements, as I will get quite an extended list of shape properties. This is what I mean:
MyClass.propTypes = {
...,
listOfItems: PropTypes.arrayOf(PropTypes.shape({
... //list of properties
}))
}
Please share your thought how would you define an array of objects with certain properties without writing each property of an object.
Share Improve this question asked Jan 6, 2018 at 16:12 Sveta BubenSveta Buben 1011 silver badge4 bronze badges 03 Answers
Reset to default 2You can try PropTypes.arrayOf(React.PropTypes.element)
or using Shape is a good approach when es to readability, you can easily understand what are the different types you can expect in that array.
what fixed for me is to change oneOfType
to oneOf
, not sure if thats for your case as well.
PropTypes.shape(...) also matches an object with additional properties as long as the specified properties are present.
So, it isn't necessary to write each property when there is an extended list of shape properties.
To validate an array of class 'Event' instances or an array of class 'Venue' instances:
MyClass.propTypes = {
listOfItems: PropTypes.arrayOf(PropTypes.shape({
type: PropTypes.oneOf([Event, Venue])
}))
}
本文标签: javascriptPropTypes array of one of the class instanceStack Overflow
版权声明:本文标题:javascript - PropTypes: array of one of the class instance - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312327a2451121.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论