1

I have a nested JSON, by which I am making a UI, I have successfully created the first part, Issue I am facing is for the second part.

What I am trying to do

I have a nested json by which I am showing parent element, now what I want to do is on click of any parent it should show that particular data.

suppose I click on parent1 so it should show child of parent one.

My JSON data

    [
  {
    "name": "parent1",
    "url": "url1",
    "child": [
      {
        "name": "child1",
        "url": "child_url1",
        "grand_child": [
          {
            "name": "some name",
            "url": "some url"
          }
        ]
      },
      {
        "name": "child2",
        "url": "child_url2"
      },
      {
        "name": "child3",
        "url": "child_url3"
      }
    ]
  },
  {
    "name": "parent2",
    "url": "url2",
    "child": [
      {
        "name": "child22",
        "url": "child_url22"
      }
    ]
  },
  {
    "name": "parent3",
    "url": "url3",
    "child": [
      {
        "name": "child33",
        "url": "child_url33"
      },
      {
        "name": "child44",
        "url": "child_url44"
      },
      {
        "name": "child55",
        "url": "child_url55"
      }
    ]
  }
]

What I am doing

<div className="row">
            {d.map((li) => (
                <div className="col-12 col-sm-12 col-md-6 col-lg-4 col-xl-4">
                    <div onClick={() => on_click}>{li.name}</div>
                </div>
            ))}
        </div>

But now I don't know how to show other part after onClick how to have that particular data.

What I am trying to acheive is something like this Code sandbox

in my code sandbox I am just showing parent3 children (assuming that is clicked at that time)

5 Answers 5

1

As per my understanding you want to show the children elements when parent is clicked & all code resides in a single react function component.

So I've changed your code little bit from the Code Sandbox & used React State Hooks to achieve you feature. Check the code sample : Code Sandbox Demo

Hope this is what you're looking for. Peace

Sign up to request clarification or add additional context in comments.

5 Comments

hey the same i want to do with json data dynamically.
@manishthakur please check the updated example from code sandbox with dynamic data.
I am testing it, with real data let me test at the fullest
What do you mean by blank space?
Hey could you please help me on this stackoverflow.com/questions/63814645/… I am stuck here from long time now
0

I am assuming you are getting your json as a prop to the component.You need to call the renderParent function that would handle the rendering of the complete json including the child array.

function renderChild(child) {
    return child && child.map((childItem)=>(
      <div>{childItem.name}</div>
    ))
  }

  function renderParent() {
    return props.data && props.data.map(item => ( <div>
    <div>{item.name}</div>
    // add your parent json data here for rendering
    <div>{renderChild(item.child)}</div>
    </div>));
  }

return (
  <div>
  {renderParent()}
  </div>
);

1 Comment

I have data in same component, and that data i want to render in that same component, parent i am able to now I want to render child on click of respective parent
0

Use the pre tag and json string to it.

<pre>{JSON.stringify(YOUR_JSON)}</pre>

Comments

0

You can show nested json element using divBody value

 let divBody = nestedJsonArray.map(item => (
              <div>
                <div>{item.url}</div>
                <div>{item.name}</div>
                <div>
                 {
                    item.child.map(citem=>(
                      <div>{citem.name}</div>
                    ))
                 }
                </div>
              </div>
            ));

 

Comments

0

Check this out...you need to save current parent child on parent click, and i am saving in the state and then showing only that child, here is code sandbox link: https://codesandbox.io/s/priceless-robinson-yk03j?file=/src/App.js:0-2010

import React, { useState } from "react";
import "./styles.css";
import "bootstrap/dist/css/bootstrap.min.css";

const data = [
  {
    name: "parent1",
    url: "url1",
    child: [
      {
        name: "child1",
        url: "child_url1",
        grand_child: [
          {
            name: "some name",
            url: "some url"
          }
        ]
      },
      {
        name: "child2",
        url: "child_url2"
      },
      {
        name: "child3",
        url: "child_url3"
      }
    ]
  },
  {
    name: "parent2",
    url: "url2",
    child: [
      {
        name: "child22",
        url: "child_url22"
      }
    ]
  },
  {
    name: "parent3",
    url: "url3",
    child: [
      {
        name: "child33",
        url: "child_url33"
      },
      {
        name: "child44",
        url: "child_url44"
      },
      {
        name: "child55",
        url: "child_url55"
      }
    ]
  }
];

export default function App() {
  const [child, setChild] = useState([]);
  const [currentParent, setCurrentParent] = useState("");

  const on_click = name => {
    const parent = data.filter(parent => parent.name === name);
    console.log(parent);
    setCurrentParent(parent[0].name);
    setChild(parent[0].child);
  };

  return (
    <div className="App">
      <div className="row">
        {data.map((parent, i) => (
          <div className="col-4 col-sm-4 col-md-4 col-lg-4 col-xl-4" key={i}>
            <div onClick={() => on_click(parent.name)}>{parent.name}</div>
            {currentParent === parent.name &&
              child.map((data, i) => (
                <div key={i}>
                  {data.name}{" "}
                  {data.grand_child &&
                    data.grand_child.map((gc, i) => (
                      <li className="pl-4 ml-4" key={i}>
                        {gc.name}
                      </li>
                    ))}
                  <br />
                </div>
              ))}
          </div>
        ))}
      </div>
    </div>
  );
}

2 Comments

hey you have not done it for grand child, suppose child1 has some grand child but when I click there nothing is happening ?
thanks for pointing out the issue, i have updated the code with grand child.

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.