我是ReactJS的新手。我只是在玩组件而且我不确定我做错了什么。我有2个数据源,类别&子类别。我想在DIV中渲染我的主要类别,然后我想要它自己的DIV中的每个子类别以及它可能的链接选择列表。最终,当您单击主类别时,它将显示子类别。现在我只是想把它全部正确显示
var Navigation = React.createClass({
render: function() {
return (
<div className="navigation">
<NavigationCategoryList data={this.props.category} />
<NavigationSubCategoryList data={this.props.category} subData={this.props.subCategory} />
</div>
);
}
});
var NavigationCategoryList = React.createClass({
render: function () {
var links = this.props.data.map(function(category) {
return (
<NavigationCategory name={category.name} link={category.link} />
);
});
return (
<div>
<div id="logo">Test</div>
<div className="navigationCategory">
{links}
</div>
</div>
);
}
});
var NavigationSubCategoryList = React.createClass({
render: function () {
var sub = this.props.data.map(function(subcategory) {
return (
<NavigationSubCategoryLinks name={subcategory.name} link={subcategory.link} subCategory={subCategory} />
);
});
return (
<div className="subCategoryContainer">
{sub}
</div>
);
}
});
var NavigationSubCategoryLinks = React.createClass({
render: function () {
return (
<div className="navigationSubCategory" id={this.props.name}>
{this.props.name} links
</div>
);
}
});
var NavigationCategory = React.createClass({
render: function () {
return (
<div className="navigationLink">
<a href={this.props.link}>{this.props.name}</a>
</div>
);
}
});
var category = [
{"id": 1, "name": "Home", "link": "#"},
{"id": 2, "name": "About", "link": "#"},
{"id": 3, "name": "Links", "link": "#"},
{"id": 4, "name": "Contact", "link": "#"}
];
var subCategory = [
{"id": 1, "parent": "Home", "name": "Home link 1", "link": "#"},
{"id": 2, "parent": "Home", "name": "Home link 2", "link": "#"},
{"id": 3, "parent": "Home", "name": "Home link 3", "link": "#"},
{"id": 4, "parent": "Home", "name": "Home link 4", "link": "#"},
{"id": 5, "parent": "About", "name": "About link 1", "link": "#"},
{"id": 6, "parent": "About", "name": "About link 2", "link": "#"},
{"id": 7, "parent": "About", "name": "About link 3", "link": "#"},
{"id": 8, "parent": "About", "name": "About link 4", "link": "#"},
{"id": 9, "parent": "links", "name": "links link 1", "link": "#"},
{"id": 10, "parent": "links", "name": "links link 2", "link": "#"},
{"id": 11, "parent": "links", "name": "links link 3", "link": "#"},
{"id": 12, "parent": "Contact", "name": "Contact link 1", "link": "#"}
];
ReactDOM.render(
<Navigation category={category} subCategory={subCategory} />,
document.getElementById('example')
);
答案 0 :(得分:0)
你需要两件事
现在,SubCategoryList将仅显示将其父级作为所选类别的项目。 https://jsfiddle.net/69z2wepo/30587/
var Navigation = React.createClass({
getInitialState: function() {
return {selected_category: 'Home'};
},
handleChange(e){
this.setState({selected_category: e.target.firstChild.data});
},
render: function() {
return (
<div className="navigation">
<NavigationCategoryList data={this.props.category} handleChange={this.handleChange}/>
<NavigationSubCategoryList data={this.props.category} subData={this.props.subCategory} selected_category={this.state.selected_category} />
</div>
);
}
});
var NavigationCategoryList = React.createClass({
render: function () {
var handleChange = this.props.handleChange;
var links = this.props.data.map(function(category) {
return (
<NavigationCategory name={category.name} link={category.link} handleChange={handleChange}/>
);
});
return (
<div>
<div id="logo">Test</div>
<div className="navigationCategory">
{links}
</div>
</div>
);
}
});
var NavigationSubCategoryList = React.createClass({
render: function () {
var selected = this.props.selected_category;
var sub = this.props.subData.map(function(subcategory) {
if(subcategory.parent === selected)
return (
<NavigationSubCategoryLinks name={subcategory.name} link={subcategory.link} subCategory={subCategory} />
);
});
return (
<div className="subCategoryContainer">
{sub}
</div>
);
}
});
var NavigationSubCategoryLinks = React.createClass({
render: function () {
return (
<div className="navigationSubCategory" id={this.props.name}>
{this.props.name} links
</div>
);
}
});
var NavigationCategory = React.createClass({
render: function () {
var handleChange = this.props.handleChange;
return (
<div className="navigationLink">
<a href={this.props.link} onClick={handleChange}>{this.props.name}</a>
</div>
);
}
});
var category = [
{"id": 1, "name": "Home", "link": "#"},
{"id": 2, "name": "About", "link": "#"},
{"id": 3, "name": "Links", "link": "#"},
{"id": 4, "name": "Contact", "link": "#"}
];
var subCategory = [
{"id": 1, "parent": "Home", "name": "Home link 1", "link": "#"},
{"id": 2, "parent": "Home", "name": "Home link 2", "link": "#"},
{"id": 3, "parent": "Home", "name": "Home link 3", "link": "#"},
{"id": 4, "parent": "Home", "name": "Home link 4", "link": "#"},
{"id": 5, "parent": "About", "name": "About link 1", "link": "#"},
{"id": 6, "parent": "About", "name": "About link 2", "link": "#"},
{"id": 7, "parent": "About", "name": "About link 3", "link": "#"},
{"id": 8, "parent": "About", "name": "About link 4", "link": "#"},
{"id": 9, "parent": "links", "name": "links link 1", "link": "#"},
{"id": 10, "parent": "links", "name": "links link 2", "link": "#"},
{"id": 11, "parent": "links", "name": "links link 3", "link": "#"},
{"id": 12, "parent": "Contact", "name": "Contact link 1", "link": "#"}
];
ReactDOM.render(
<Navigation category={category} subCategory={subCategory} />,
document.getElementById('example')
);