[英]What is best way to convert this with JSON data in React JS?
I have React TreeView right now I'm using as static but now I want to use this with JSON format how can I done this?我现在有 React TreeView 我正在使用静态但现在我想将它与 JSON 格式一起使用我该怎么做?
My Code:-我的代码:-
ratHelper.js ratHelper.js
const getData = () => { return [ { "tree": { "content": "main", "type": "L1", "tree": [ { "content": "subtree with children", "type": "L2", }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" }, { "content": "hello", "type": "L2" } ] } } ] } export default {getData}
import React from "react"; import Tree from "react-animated-tree"; import "./rat.css"; import helper from "./ratHelper"; const treeStyles = { position: "absolute", top: 40, left: 40, color: "white", fill: "white", width: "100%", height: "100vh", overflow: "auto", }; const typeStyles = { fontSize: "5em", verticalAlign: "middle", }; const data = helper.getData(); console.log(data); const ReactAnimatedTree = () => ( <> {/* {data.map((p)=>{ <Tree>{p}</Tree> })} */} <Tree content="main" type="L1" canHide open style={treeStyles}> <Tree content="subtree with children" type="L2"> <Tree content="sub-subtree with children" type="L3"> <Tree content="child 1" type="L4"> <Tree content="child 1" type="L5"> <Tree content="child 1" type="L6"> <Tree content="child 1" type="L7"> <Tree content="child 1" type="L8"> <Tree content="child 1" type="L9"> <Tree content="child 1" type="L10"> <Tree content="child 1" type="L11"> <Tree content="child 1" type="L12"> <Tree content="child 1" type="L13"> <Tree content="child 1" type="L14"> <Tree content="child 1" type="L15"> <Tree content="child 1" type="L16"> <Tree content="child 1" type="L17"> <Tree content="child 1" type="L18"> <Tree content="child 1" type="L19"> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> <Tree content="child 1" type="L20" /> </Tree> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> <Tree content="child 1" type="L19" /> </Tree> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> <Tree content="child 1" type="L18" /> </Tree> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> <Tree content="child 1" type="L17" /> </Tree> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> <Tree content="child 1" type="L16" /> </Tree> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> <Tree content="child 1" type="L15" /> </Tree> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> <Tree content="child 1" type="L14" /> </Tree> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> <Tree content="child 1" type="L13" /> </Tree> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> <Tree content="child 1" type="L12" /> </Tree> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> <Tree content="child 1" type="L11" /> </Tree> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> <Tree content="child 1" type="L10" /> </Tree> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> <Tree content="hello" type="L9" /> </Tree> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> <Tree content="hello" type="L8" /> </Tree> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> <Tree content="hello" type="L7" /> </Tree> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> <Tree content="hello" type="L6" /> </Tree> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> <Tree content="hello" type="L5" /> </Tree> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> <Tree content="hello" type="L4" /> </Tree> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> <Tree content="hello" type="L3" /> </Tree> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> <Tree content="hello" type="L2" /> {/* <Tree content="hello" canHide /> */} </Tree> </> ); export default ReactAnimatedTree;
Thanks for your efforts!感谢您的努力!
I think you want to know how to map the JSON data in react.我想你想知道如何在反应中映射 JSON 数据。 Here is how.这里是如何。 This is your JSON format.这是您的 JSON 格式。 I re-wrote it, so you won't mistake the data structure.我重写了它,所以你不会弄错数据结构。 It should be array.它应该是数组。
const data = [
{
tree: {
content: "main",
type: "L1",
tree: [
{
content: "subtree with children",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
{
content: "hello",
type: "L2",
},
],
},
},
];
export default data;
<div>
{data.map((tree,index)=> (
<Tree type={tree?.type} key={index} content={tree?.content}>
{tree?.tree.map((subTree,index)=> (
<Tree type={subTree?.type} key={index} content={subTree?.content}>
// you can continue map more subSubTree
</Tree>
))}
</Tree>
))}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.