简体   繁体   English

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

[英]Warning: Failed propType: Invalid prop of type `array` expected `object` with React

So this line of code is throwing something like ' Failed propType: Invalid prop of type array expected object .' 因此,这行代码抛出的内容类似于“Failed propType:类型array预期object无效prop”。

Why is this happening? 为什么会这样?

Here is my JSON: 这是我的JSON:

"student_records": [
      {
        "program": "PSCI-210",
        "grade": 80
      }
    ]

jsx: JSX:

import React, { PropTypes } from 'react';


const StudentRecordPropTypes = {
  studentRecordData: PropTypes.object.isRequired,
};

function StudentRecord(props) {
    const Records = props.studentRecordData;


  return (
    <div>
        {(Records || []).map(student_records => (
              <ul>
                    <li>{student_records.program} : {student_records.grade} </li>
              </ul>
            ))}
    </div>
  );
}
StudentRecord.propTypes = StudentRecordPropTypes;

export default StudentRecord;

It displays correctly. 它显示正确。 After some googling, I realized that its looking to an array but its infact an object. 经过一些谷歌搜索,我意识到它寻找一个数组,但它实际上是一个对象。 My problem is that I don't know how to fix it. 我的问题是我不知道如何解决它。 How can I remove this error? 如何删除此错误?

Change 更改

const StudentRecordPropTypes = {
  studentRecordData: PropTypes.object.isRequired,
};

to

const StudentRecordPropTypes = {
  studentRecordData: PropTypes.array.isRequired,
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:propType失败:提供给“ LaptopHandling”的类型为“ function”的无效prop`swimmingSnapshot`, - Warning: Failed propType: Invalid prop `swimmingSnapshot` of type `function` supplied to `LaptopHandling`, 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom 警告:道具类型失败:提供给“提供者”的对象类型为“对象”的道具儿童无效,需要一个ReactElement - Warning: Failed prop type: Invalid prop `children` of type `object` supplied to `Provider`, expected a single ReactElement 反应错误'Failed propType:提供给`Provider`的无效道具`children`,期望一个ReactElement' - React error 'Failed propType: Invalid prop `children` supplied to `Provider`, expected a single ReactElement' 警告:失败的 propType:提供给 `Route` 的 prop`component` 无效 - Warning: Failed propType: Invalid prop `component` supplied to `Route` 警告:道具类型失败:提供给“路线”的道具“组件”无效 - Warning: Failed propType: Invalid prop 'component' supplied to 'route' 警告:propType失败:提供给`sportsMockUpStandard`的无效道具`rrrr` - Warning: Failed propType: Invalid prop `rrrr` supplied to `sportsMockUpStandard` 反应如何修复失败的道具类型 - 类型字符串预期对象的无效道具 - React How to fix Failed prop type - Invalid prop of type string expected object 道具类型失败:提供给“ForwardRef(DataGrid)”的“object”类型的无效道具“rows”,预期为“array” - Failed prop type: Invalid prop `rows` of type `object` supplied to `ForwardRef(DataGrid)`, expected `array` 无效的道具:道具“数据”的类型检查失败。 预期数组,得到对象 - Invalid prop: type check failed for prop "data". Expected Array, got Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM