我正在做一個關於創建樹的反應的教程。 比如這個變量被表示爲一棵樹:React樹 - 如何刪除當前節點?
var tree = {
title: "howdy",
childNodes: [
{title: "bobby"},
{title: "suzie", childNodes: [
{title: "puppy", childNodes: [
{title: "dog house"}
]},
{title: "cherry tree"}
]}
]
};
我想補充旁邊它刪除當前節點及其子節點每個節點的按鈕。
var TreeNode = React.createClass({
getInitialState: function() {
return {
visible: true
};
},
handleDeleteClick: function() {
//What should I place here?
},
render: function() {
console.log(this.state);
var childNodes;
var classObj;
if (this.props.node.childNodes != null) {
childNodes = this.props.node.childNodes.map(function(node, index) {
return <li key={index}><TreeNode node={node} /></li>
});
classObj = {
togglable: true,
"togglable-down": this.state.visible,
"togglable-up": !this.state.visible
};
}
var style;
if (!this.state.visible) {
style = {display: "none"};
}
return (
<div>
<h5 onClick={this.toggle} className={React.addons.classSet(classObj)}>
{this.props.node.title}
<span className="input-group-btn">
<button className="btn btn-default" onClick={this.handleDeleteClick}>
Delete
</button>
</span>
</h5>
<ul style={style}>
{childNodes}
</ul>
</div>
);
},
toggle: function() {
this.setState({visible: !this.state.visible});
}
});
我該怎麼做? 不幸的是,我不能使用數據庫作爲後端(我可以保持沒有數據庫的狀態嗎?)