react 树递归

前端之家收集整理的这篇文章主要介绍了react 树递归前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

var data = [{
name:1,
next:[{name:4},{name:999}]
},{name:2},{name:3}]

function clickMod(){
return function(){
alert("hhhhhhhhh")
}
}

function styleName(){
return {
background:'green',
color:'red',
marginBottom:'50px'
}
}

function createTreeDate(data){
if(data){
let lis=[];
for(let index in data){
console.log("index:",index)
let childrenDiv = null;

if(data[index].next){
let next= createTreeDate(data[index].next);
childrenDiv=<div onClick={clickMod()}>{next}</div>;
}

let li = <li style={styleName()} key={index}>
<div>{data[index].name}</div>
{childrenDiv}
</li>;

lis.push(li);
}
return <ul>{lis}</ul>
}

}

let treeHtml =createTreeDate(data); return ( <div> {treeHtml} </div> )

猜你在找的React相关文章