我在很多组件中使用了几个大对象,所以我为每个大对象创建了一个proptypes文件,如下所示:
PropTypes/PropLargeObject.js
其中包含 从" prop-types";
导入PropTypesconst 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。我在这里做错了什么?
答案 0 :(得分:1)
删除不必要的花括号:
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
}