Love Cat
Love Cat

Reputation: 23

How to pass json values between React component

I have a question about passing the json data between, I have there components PassTestData/ComA/ComB, the data is imported in PassTestData and want to pass to ComA. However, there is an error message 'TypeError: data.map is not a function'. The following are my codes.

PassTestData

Import data from ‘../data.json’
const PassTestData = () => {
  return (
    <div>
      < ComA data={data} />
    </div>
  );};

ComA

const ComA = props => {
  const data = props;
  return (
    <Card>
      <CardTitle>
        <EmailViewHeader />
      </CardTitle>
      <Table hover>
        <thead>
          <tr>
            <th>
              Title A
            </th>
            <th>
              Title B
            </th>
            <th>
              Title C
            </th>
            <th>
              Title D
            </th>
            <th>
              Title E
            </th>
          </tr>
        </thead>
        <tbody>
          {data.map(row => (
            <ComB
              A={row.A}
              B={row.B}
              C={row.C}
              D={row.D}
              E={row.E}
            />
          ))}
        </tbody>
      </Table>
    </Card>
  );
};
export default ComA;

ComB

const ComB = props => {
  const { A, B, C, D, E } = props;
  return (
    <tr>
      <td>{A}</td>
      <td>{B}</td>
      <td>{C}</td>
      <td>{D}</td>
      <td>{E}</td>
    </tr>
  );
};

export default ComB ;

data.json

[
    {
      "A": "dataA",
      "B": "dataB",
      "C": "dataC",
      "D": "dataD",
      "E": "dataE"
    },
    {
      "A": "dataA_2",
      "B": "dataB_2",
      "C": "dataC_2",
      "D": "dataD_2",
      "E": "dataE_2"
    },
   ]

Thank you!

Upvotes: 1

Views: 40

Answers (3)

j-petty
j-petty

Reputation: 3026

You need to define const data = props; like:

const { data } = props;

OR

const data = props.data;

TypeError: data.map is not a function is thrown due to props being an Object not an Array.

Array.prototype.map expects an Array.

Upvotes: 0

Sohail Ashraf
Sohail Ashraf

Reputation: 10604

Convert you json to object and then pass it in props. JSON is a string and the map function is not defined in it.

Import data from ‘../data.json’
const PassTestData = () => {
 let _data = JSON.parse(data);  
  return (
    <div>
      < ComA date={_date} />
    </div>
  );};

And in component A pass the props data like this.

const ComA = props => {
  const data = props.data;
  return (
    ....
  );
};
export default ComA

Upvotes: 0

akhtarvahid
akhtarvahid

Reputation: 9779

Define like const data = props.data; or const {data}= props;

const ComA = props => {
  const data = props.data;
  return (
    <Card>
      <CardTitle>
        <EmailViewHeader />
      </CardTitle>
      <Table hover>
        <thead>
          <tr>
            <th>
              Title A
            </th>
            <th>
              Title B
            </th>
            <th>
              Title C
            </th>
            <th>
              Title D
            </th>
            <th>
              Title E
            </th>
          </tr>
        </thead>
        <tbody>
          {data.map(row => (
            <ComB
              A={row.A}
              B={row.B}
              C={row.C}
              D={row.D}
              E={row.E}
            />
          ))}
        </tbody>
      </Table>
    </Card>
  );
};

Upvotes: 1

Related Questions