0

I have JSON data like this. I want to render the child object title. But it says "index.js:1 Warning: Each child in a list should have a unique "key" prop."

[
  {
    "title": "Components",
    "children": [
      {
        "title": "Buttons",
        "url": "url"
      }
    ]
  },
  {
    "title": "Components",
    "children": [
      {
        "title": "Buttons",
        "url": "url"
      }
    ]
  }
]

How can I get children title? {data.children.title} does not work.

const items = data.map(data => {
        return(
            <div>
                <ul>
                    <li>
                        <span>{data.title}</span>
                        <span>{data.children.title}</span>
                    </li>
                </ul>
            </div>
        )
    });

    return items;
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
paws
  • 91
  • 1
  • 10

1 Answers1

1

Assuming your children sub array always has at least one item, then you could render the title of the first child item of the current data object by the following:

data.children[0].title

In your render code, that'd look like this:

const items = data.map((data, idx) => (
    <div key={idx}>
        <ul>
            <li>
                <span>{data.title}</span>
                <span>{data.children[0].title}</span>
            </li>
        </ul>
    </div>));

return items;

For a more robust alternative, you could check for the existance of an item in the children sub-array before rendering by adding chaining a call to filter() into your rendering logic:

const items = data
/* If children sub-array is non-empty, then it is included
for rendering */
.filter(data => data.children.length > 0)
/* Map any data items that pass filtering */
.map((data, idx) => (
    <div key={idx}>
        <ul>
            <li>
                <span>{data.title}</span>
                <span>{data.children[0].title}</span>
            </li>
        </ul>
    </div>));

return items;

Note also, I've include the key prop on each div that has been mapped of each mapped as is required when rendering lists, which should resolve the warning that you're seeing in the console. Hope that helps!

Update

To render all children per data object as a sub-list inside of the ul you could do so via an inner mapping of data.children to <li> elements like this:

const items = data
.filter(data => data.children.length > 0)
.map((data, idx) => (
    <div key={idx}>
        {/* 
        I'm assuming data.title should exist outside of 
        children list 
        */}
        <span>{data.title}</span>
        <ul> 
        {   data
            .children
            .map((child, jdx) => (
            <li key={jdx}>
                <span>{child.title}</span>
            </li>))
        }
        </ul>
    </div>));
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
  • You will have to give `key` to span tag. Not the `div` – Niranjan N Raju May 20 '20 at 09:52
  • Thanks! It's work but just one child. How can i get child list? ``` [ { "title": "Components", "children": [ { "title": "Buttons", "url": "url" }, { "title": "Inputs", "url": "url" } ] }, { "title": "Components", "children": [ { "title": "241451", "url": "urdsfl" } ] } ] ``` – paws May 20 '20 at 22:08
  • @paws you're welcome :-) I've just updated the answer, hope that helps! – Dacre Denny May 20 '20 at 22:19
  • @DacreDenny thanks a lot! i can speak english just a little but you can understand me! I'm grateful ! – paws May 20 '20 at 22:23
  • @paws you are very welcome :-) – Dacre Denny May 20 '20 at 22:48
  • @DacreDenny can i ask another question? – paws May 22 '20 at 07:19
  • Yes certainly, if you post a new question and send the link to me here I will be happy to help – Dacre Denny May 22 '20 at 10:36
  • @DacreDenny thank you ! :) https://stackoverflow.com/questions/61955420/react-toggle-class – paws May 22 '20 at 12:48
  • @DacreDenny could you take a look? https://stackoverflow.com/questions/63929343/react-npm-start-doesnt-work-after-create-react-app – paws Sep 17 '20 at 00:03