Answer the question
In order to leave comments, you need to log in
How to make a vertical menu in React with a menu item highlighted?
Good afternoon. I'm new to react, so I'm hoping for your advice
. What I mean:
got an array, drew a menu on it
render() {
return (
<ul className="nav nav-pills nav-stacked ">
{this.props.itemList.map(
function (item) {
return <MenuItem item={item} />
}
)}
</ul>
)
};
Answer the question
In order to leave comments, you need to log in
It is necessary that when you click on a menu item, it is highlighted in a different color. It all worked out.
class MyMenu extends React.Component {
constructor(props) {
super(props);
this.handleItemClick = this.handleItemClick.bind(this);
this.state = { selectedItem: null };
}
handleItemClick(selectedItem) {
console.log('handleItemClick', selectedItem);
this.setState({ selectedItem });
}
render() {
const { items } = this.props;
const { selectedItem } = this.state;
const { handleItemClick } = this;
return (
<ul>
{ items.map( (item, key) => (
<MenuItem
key={key}
item={item}
isActive={item === selectedItem}
onClick={handleItemClick}
/>
)) }
</ul>
);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question