[英]React PropTypes import class. Prop type is invalid
我在许多组件中使用了几个大对象,因此我为每个大对象制作了一个原型文件,如下所示:
PropTypes/PropLargeObject.js
其中包含从“ prop-types”导入的PropTypes;
const PropLargeObject =
PropTypes.shape({
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
items: PropTypes.ArrayOf(PropTypes.Shape({
itemId: PropTypes.number.isRequired,
itemName: PropTypes.string.isRequired
}))
});
export default PropLargeObject;
我在组件中使用该对象,如下所示:
import {PropLargeObject} from "./PropTypes/PropLargeObject";
Component.propTypes = {
LargeObject: {PropLargeObject}
}
它警告我Prop类型“ PropLargeObject”无效,它必须是一个函数,通常来自React.PropTypes。 我在这里做错了什么?
删除不必要的花括号:
import PropLargeObject from "./PropTypes/PropLargeObject"; // PropLargeObject is the default export
Component.propTypes = {
LargeObject: PropLargeObject // PropLargeObject is a PropTypes.shape function. Don't wrap with an object
}
您甚至可以将其短路一点:
import LargeObject from "./PropTypes/PropLargeObject"; // you can assign whatever name you want to default imports
Component.propTypes = {
LargeObject
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.