DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

1

Tree In React

import React, {useState} from 'react'
import ReactDOM from 'react-dom'

function App() {
  const data = [
    {
      name: 'Node-1',
      id: 1,
      parent_id: null,
    },
    {
      name: 'Node-2',
      id: 2,
      parent_id: null,
    },
    {
      name: 'Node-1-1',
      id: 3,
      parent_id: 1,
    },
    {
      name: 'Node-1-2',
      id: 4,
      parent_id: 1,
    },
    {
      name: 'Node-1-1-1',
      id: 5,
      parent_id: 3,
    },
    {
      name: 'Node-1-1-2',
      id: 6,
      parent_id: 3,
    },
    {
      name: 'Node-1-2-1',
      id: 7,
      parent_id: 4,
    },
    {
      name: 'Node-2-1',
      id: 8,
      parent_id: 2,
    },
    {
      name: 'Node-2-2',
      id: 9,
      parent_id: 2,
    },
  ]
  // dependency

  return <Tree data={data} />
}

const TreeNode = ({node, data, onToggle}) => {
  const hasData = data.some((item) => item.parent_id === node.id)

  return (
    <div>
      <div onClick={() => onToggle(node.id)}>
        {hasData && <span>{node.isExpanded ? ' - ' : ' + '}</span>}
        {node.name}
      </div>
      {node.isExpanded ? (
        <div>
          {data
            .filter((item) => item.parent_id === node.id)
            .map((eachChild) => (
              <TreeNode
                key={eachChild.id}
                node={eachChild}
                data={data}
                onToggle={onToggle}
              />
            ))}
        </div>
      ) : (
        <></>
      )}
    </div>
  )
}

const Tree = ({data}) => {
  const [treeData, setTreeData] = useState(data)

  const handleToggle = (id) => {
    setTreeData((prevData) =>
      prevData.map((each) =>
        each.id === id ? {...each, isExpanded: !each.isExpanded} : each,
      ),
    )
  }

  return (
    <div>
      {treeData
        ?.filter((eachData) => eachData.parent_id === null)
        .map((eachNode) => (
          <TreeNode
            key={eachNode.id}
            node={eachNode}
            data={treeData}
            onToggle={handleToggle}
          />
        ))}
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))

Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay