Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.'

Why is this happening?

Here is my JSON:

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

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?

like image 564
Modelesq Avatar asked May 06 '16 02:05

Modelesq


1 Answers

Change

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

to

const StudentRecordPropTypes = {
  studentRecordData: PropTypes.array.isRequired,
};
like image 105
QoP Avatar answered Sep 20 '22 04:09

QoP