0
我想用树形数据构建一棵树。 我自己编写的代码如下React js html append
import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import { Draggable, Droppable } from 'react-drag-and-drop';
import './index.css';
const styles = {
fontFamily: 'sans-serif',
textAlign: 'center',
};
class App extends React.Component
{
constructor()
{
super();
this.html=[];
this.myTreedata=[{
name : "Phrase",
id : 1,
children : [{
name : "poem",
id : 2
}]
}];
this.state={
toggle : true
}
this.onDrop=this.onDrop.bind(this);
this.assign=this.assign.bind(this);
}
assign(data)
{
data.map(node => {
this.html.push(<Droppable types={['yolo']} onDrop=
{this.onDrop}><li id={node.id}><span>{node.name}</span>
</li></Droppable>);
if(node.children && node.children.length > 0)
{
this.html.push(<ul>);
this.assign(node.children);
this.html.push(</ul>);
}
console.log(this.html);
})
//console.log(this.html);
//return html;
}
onDrop(data,e)
{
this.setState({
toggle : !this.toggle
});
this.myTreedata=[{
name : "op",
id : 1,
children : [{
name : "poem",
id : 2,
children : [{
name : "poem2",
id : 3
}]
}]
}];
}
render()
{
var str=this.myTreedata;
this.assign(str);
return (
<div>
<Tree />
<div className="tree" >
<ul >
{this.html}
</ul>
</div>
</div>);
}
}
class Drag extends React.Component
{
constructor()
{
super();
}
render()
{
const myTreedata=[{
name : "phrase",
id : 1,
children : [{
name : "poem",
id : 2
}]
}];
return(<div><App /></div>);
}
}
const Tree =() =>
(<div>
<Draggable type="yolo" data="banana">
<p>Banana</p>
</Draggable>
</div>);
render(<Drag />, document.getElementById('root'));
但是我得到的输出是不是我wanted.Its不使所述 this.html数组元素。 基本上我试图动态追加HTML组件 我该如何处理?
会不会被叫?我看过定义,但是什么时候被调用? –