您不应该使用redux存储来处理像项目突出显示状态这样的小事情。这样想吧。您的商店在那里存储来自服务器的数据。应该在组件的状态下处理像组件一样在组件上发生变化的小东西,如颜色或类名。
class MyComponent extends React.Component {
constructor(){
super();
this.state = {highlighted: 1};
this.handleHighlightClick = this.handleHighlightClick.bind(this);
}
handleHighlightClick(e, val){
//set state here for your highlight
e.preventDefault();
this.setState({highlighted: val})
}
render(){
// for the sake of this example i'll just show a list of items to render
let menuItems = [{color: 'red'},{color: 'orange'},{color: 'yellow'},{color: 'white'}];
return (
{menuItems.map((data, key) =>
<MenuItem
data={data} // data is the color object from the list
highlighted={key===this.state.highlighted} // the key of which one is highlighted gets stored in local state
onClick={(e) => { this.handleHighlightClick(e, key)} } /> // inline function to pass through the key of the exact menu item so that you can save its key to state
)}
);
}
}
现在这只是一个简单的例子,并不包括你正在尝试做的一切。但其约90%的你想要做的,所以我会让你完成最后的10%:)
谢谢。我的问题实际上与另一个问题有关(http://stackoverflow.com/questions/36874685/how-to-build-a-side-toggle-menu-respond-to-header-menu-in-react):可以你帮忙? – Kuan