admin管理员组文章数量:1320661
I'm building a navigation bar with React. The Navigation bar has various navigation items. When one of the navigation items is clicked, the class 'active' has to be added to the clicked item (this part is easily achievable with a new state tracking if it's active).
However when the 'active' class is added to one of the navigation items, the active class has to be removed from any other navigation items currently having it. This is straightforward with jQuery, but I'm guessing thats not the React best practice. What's the ideal way to remove the 'active' class from the sibling navigation items?
I'm building a navigation bar with React. The Navigation bar has various navigation items. When one of the navigation items is clicked, the class 'active' has to be added to the clicked item (this part is easily achievable with a new state tracking if it's active).
However when the 'active' class is added to one of the navigation items, the active class has to be removed from any other navigation items currently having it. This is straightforward with jQuery, but I'm guessing thats not the React best practice. What's the ideal way to remove the 'active' class from the sibling navigation items?
Share Improve this question asked Nov 6, 2015 at 15:25 Amal AntonyAmal Antony 6,83715 gold badges54 silver badges77 bronze badges 2- 1 I think that you should resolve this on container level (Navigation bar) rather than in navigation item. Navigation Bar should know about its children. Items(children of navigation bar) don't need to know about each other. – hex13 Commented Nov 6, 2015 at 15:33
- Talk is cheap. I have shown the code below ;) – hex13 Commented Nov 6, 2015 at 16:25
2 Answers
Reset to default 6I think that the best approach would be handle this in the container ponent. Menu items don't need to know about each other, but container can. So container can give to children onClick callbacks. And later in callbacks container can set its state by setState
. In render
function you just pass active
property to your MenuItem.
// ES6, React 0.14
var MenuItem = ({onClick, text, active}) => (
<button onClick={onClick} style={active? {color: 'red'} : null}>{text}</button>
);
// example of use: <MenuBar buttons={['cat', 'dog', 'penguin']}/>
class MenuBar extends React.Component {
constructor(props) {
super(props);
this.state = {activeIndex: 0};
}
handleItemClick(index) {
this.setState({activeIndex: index});
}
render() {
var activeIndex = this.state.activeIndex;
return <div>
{
this.props.buttons.map(
(btn, i) => (
<MenuItem
active={activeIndex === i}
key={i}
onClick={this.handleItemClick.bind(this, i)}
text={btn} />
)
)
}
</div>
}
}
I've started learning React recently so I'm not an expert. But I just started building some tabs and you can see my example here.
What I did was make a conditional isActive state that changed on a click event. Then the class was changed like so:
className={this.props.isActive ? "active" : null}
本文标签:
版权声明:本文标题:javascript - [ReactJSX]: Remove CSS classes on sibling components, when the current component is clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742065257a2418793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论