gpt4 book ai didi

javascript - 仅切换 Reactjs 中单击的菜单

转载 作者:行者123 更新时间:2023-12-03 14:29:58 25 4
gpt4 key购买 nike

我正在使用递归函数制作菜单和子菜单,我需要帮助才能仅打开相应的菜单和子菜单。

对于按钮和折叠 Reactstrap已使用..

执行菜单填充的递归函数:

{this.state.menuItems &&
this.state.menuItems.map((item, index) => {
return (
<div key={item.id}>
<Button onClick={this.toggle.bind(this)}> {item.name} </Button>
<Collapse isOpen={this.state.isToggleOpen}>
{this.buildMenu(item.children)}
</Collapse>
</div>
);
})}

以及buildMenu函数如下,

  buildMenu(items) {
return (
<ul>
{items &&
items.map(item => (
<li key={item.id}>
<div>
{this.state.isToggleOpen}
<Button onClick={this.toggle.bind(this)}> {item.name} </Button>
<Collapse isOpen={this.state.isToggleOpen}>
{item.children && item.children.length > 0
? this.buildMenu(item.children)
: null}
</Collapse>
</div>
</li>
))}
</ul>
);
}

到目前为止,代码没有问题,但我需要帮助才能使 menu -> submenu -> submenu 逐步打开和关闭相应级别。

工作示例:https://codesandbox.io/s/reactstrap-accordion-9epsp

您可以看一下这个示例,当您单击任何菜单时,整个菜单级别都会打开,而不是单击一个菜单。

要求

如果用户单击菜单One,则单击子菜单(子菜单)

-> One-One 

需要打开。

然后,如果用户点击One-One

 ->   One-One-One
-> One - one - two
-> One - one - three

需要打开。

同样,它是嵌套的,因此单击任何菜单/子项后,它们各自的下一级需要打开。

我是 React 和 Reactstrap 设计方式的新手,因此专业知识的任何帮助对于我继续并了解实际需要如何完成它都是有用的。

最佳答案

不要使用一个大组件,而是考虑将组件拆分成更小的组件。这样您就可以向每个菜单项添加状态以切换底层菜单项。

如果您想将所有底层菜单项重置为其默认关闭位置,则应在每次打开底层按钮时创建一个新的组件实例。通过拥有<MenuItemContainer key={timesOpened} MenuItemContainer当您“打开” MenuItem 时,将被分配一个新 key 。分配一个新的键将创建一个新的组件实例,而不是更新现有的实例。

有关详细说明,我建议阅读 You Probably Don't Need Derived State - Recommendation: Fully uncontrolled component with a key .

const loadMenu = () => Promise.resolve([{id:"1",name:"One",children:[{id:"1.1",name:"One - one",children:[{id:"1.1.1",name:"One - one - one"},{id:"1.1.2",name:"One - one - two"},{id:"1.1.3",name:"One - one - three"}]}]},{id:"2",name:"Two",children:[{id:"2.1",name:"Two - one"}]},{id:"3",name:"Three",children:[{id:"3.1",name:"Three - one",children:[{id:"3.1.1",name:"Three - one - one",children:[{id:"3.1.1.1",name:"Three - one - one - one",children:[{id:"3.1.1.1.1",name:"Three - one - one - one - one"}]}]}]}]},{id:"4",name:"Four"},{id:"5",name:"Five",children:[{id:"5.1",name:"Five - one"},{id:"5.2",name:"Five - two"},{id:"5.3",name:"Five - three"},{id:"5.4",name:"Five - four"}]},{id:"6",name:"Six"}]);

const {Component, Fragment} = React;
const {Button, Collapse} = Reactstrap;

class Menu extends Component {
constructor(props) {
super(props);
this.state = {menuItems: []};
}

render() {
const {menuItems} = this.state;
return <MenuItemContainer menuItems={menuItems} />;
}

componentDidMount() {
loadMenu().then(menuItems => this.setState({menuItems}));
}
}

class MenuItemContainer extends Component {
render() {
const {menuItems} = this.props;
if (!menuItems.length) return null;
return <ul>{menuItems.map(this.renderMenuItem)}</ul>;
}

renderMenuItem(menuItem) {
const {id} = menuItem;
return <li key={id}><MenuItem {...menuItem} /></li>;
}
}
MenuItemContainer.defaultProps = {menuItems: []};

class MenuItem extends Component {
constructor(props) {
super(props);
this.state = {isOpen: false, timesOpened: 0};
this.open = this.open.bind(this);
this.close = this.close.bind(this);
}

render() {
const {name, children} = this.props;
const {isOpen, timesOpened} = this.state;
return (
<Fragment>
<Button onClick={isOpen ? this.close : this.open}>{name}</Button>
<Collapse isOpen={isOpen}>
<MenuItemContainer key={timesOpened} menuItems={children} />
</Collapse>
</Fragment>
);
}

open() {
this.setState(({timesOpened}) => ({
isOpen: true,
timesOpened: timesOpened + 1,
}));
}

close() {
this.setState({isOpen: false});
}
}

ReactDOM.render(<Menu />, document.getElementById("root"));
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reactstrap/8.4.1/reactstrap.min.js"></script>

<div id="root"></div>

关于javascript - 仅切换 Reactjs 中单击的菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60602377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com