2017-11-02 6 views
0

私はこのような各ラージオブジェクトのためのproptypesファイルを作ったので、私は多くのコンポーネントで使用するいくつかの大規模なオブジェクトを持っている:React PropTypesインポートクラス。プロップタイプが無効です

「proptypes」から 輸入PropTypesが含まれてい
PropTypes/PropLargeObject.js 

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} 
} 

それはプロップタイプ「PropLargeObjectを」警告私を与えるが、それは通常React.PropTypesから、関数でなければなりません無効です。私はここで間違って何をしていますか?

答えて

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 
} 
+1

または "./PropTypes" からわずか 'インポート{PropLargeObject};' – Chris

関連する問題