0

OK, so Im getting this error when trying to return some data from my web api. Ive tried all as suggested on other similar posts but cant get this working. Any help appreciated.

Objects are not valid as a React child (found: object with keys {addressType, address1, address2, city, county, postCode, country, id}). If you meant to render a collection of children, use an array instead.

data...

"rows": [
        {
            "company": null,
            "controlPanel": null,
            "name": "Basement",
            "cloudAccessEnabled": true,
            "notes": "quad volcans quad quo si imaginator volcans brevens, nomen gravum Multum plorum brevens, in e quo quad",
            "addresses": [
                {
                    "addressType": "HeadOffice, Marketing",
                    "address1": "729 Rocky First Blvd.",
                    "address2": "APT 22",
                    "city": "Haverhill",
                    "county": "Dunbartonshire",
                    "postCode": "SL1 9GH",
                    "country": null,
                    "id": "62a494ee-37cf-3021-5a29-00b54adc9255"
                }
            ],
            "installers": null,
            "lastMaintenance": "2016-06-30T00:00:00",
            "nextMaintenance": "0001-01-01T00:00:00",
            "latestResult": "None Yet",
            "id": "c0d44330-8369-0e78-f45b-000142b59fa8"
        },

how I am trying to display in datatable... https://github.com/jbetancur/react-data-table-component

//this works
//cell: row => <div>{row.notes}</div>,

//doesnt work-Objects are not valid as a React child error
    cell: row => (
        <div>
          {row.addresses.map((addressType, id) => (
            <div key={id}>{addressType}</div>
          ))}
        </div>
      ),
2
  • the adressType you are passing in your map function is not what you think you are passing, that adressType is actually your addresses[i] for that iteration so what you should do is: <div key={adressType.id}>{adressType.adressType}</div>. Change the adressType in map function to a better name like address then do address.id and adress.adressType and remove the id from your map function. Commented Aug 30, 2019 at 15:43
  • Or you could do what @JojoTutor said below and deconstruct it. Commented Aug 30, 2019 at 15:45

1 Answer 1

1

It should be like this:

cell: row => (
        <div>
          {row.addresses.map(({ addressType, id }) => (
            <div key={id}>{addressType}</div>
          ))}
        </div>
      ),
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks thats fixed the Objects are not valid as a React child error, but now im getting Uncaught TypeError: Cannot read property 'map' of null
That case maybe some "row" does not have "addresses" property, try this: {row.addresses && row.addresses.map(({ addressType, id }) => ( <div key={id}>{addressType}</div> ))}
Glad to help ...!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.