javascript – 警告:propType失败:使用React的类型为`array` expected`object`的prop

前端之家收集整理的这篇文章主要介绍了javascript – 警告:propType失败:使用React的类型为`array` expected`object`的prop前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
因此,这行代码抛出的内容类似于“Failed propType:类型数组预期对象的无效prop”.

为什么会这样?

这是我的JSON

  1. "student_records": [
  2. {
  3. "program": "PSCI-210","grade": 80
  4. }
  5. ]

JSX:

  1. import React,{ PropTypes } from 'react';
  2.  
  3.  
  4. const StudentRecordPropTypes = {
  5. studentRecordData: PropTypes.object.isrequired,};
  6.  
  7. function StudentRecord(props) {
  8. const Records = props.studentRecordData;
  9.  
  10.  
  11. return (
  12. <div>
  13. {(Records || []).map(student_records => (
  14. <ul>
  15. <li>{student_records.program} : {student_records.grade} </li>
  16. </ul>
  17. ))}
  18. </div>
  19. );
  20. }
  21. StudentRecord.propTypes = StudentRecordPropTypes;
  22.  
  23. export default StudentRecord;

显示正确.经过一些谷歌搜索,我意识到它寻找一个数组,但它实际上是一个对象.我的问题是我不知道如何解决它.如何删除错误

解决方法

更改
  1. const StudentRecordPropTypes = {
  2. studentRecordData: PropTypes.object.isrequired,};

  1. const StudentRecordPropTypes = {
  2. studentRecordData: PropTypes.array.isrequired,};

猜你在找的JavaScript相关文章