gpt4 book ai didi

javascript - 如何关闭和打开侧边栏 onClick

转载 作者:行者123 更新时间:2023-11-29 20:50:36 24 4
gpt4 key购买 nike

每当我使用它时,侧边栏总是打开的,即使我没有点击按钮这样做也是如此。换句话说,每当我刷新浏览器时,侧边栏就已经打开了。我希望它被关闭,只有当我点击按钮时,侧边栏才会打开。

但我希望它根据命令打开和关闭。我尝试使用支持的 Prop ,但仍然没有运气 :(。

顺便说一句,我正在使用这个 https://github.com/balloob/react-sidebar#installation

这是我的 App.js 文件:

import React from "react";
import Sidebar from "react-sidebar";

class App extends React.Component {
constructor(props) {
super(props);
this.state = {
sidebarOpen: true
};
this.onSetSidebarOpen = this.onSetSidebarOpen.bind(this);
}

onSetSidebarOpen(open) {
this.setState({ sidebarOpen: open });
}

render() {
return (
<Sidebar
sidebar={<div><b>Sidebar content</b><b>Sidebar content</b><b>Sidebar content</b><b>Sidebar content</b><b>Sidebar content</b><b>Sidebar content</b><b>Sidebar content</b><b>Sidebar content</b></div>}
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
styles={{ sidebar: { background: "white" } }}
>
<button onClick={() => this.onSetSidebarOpen(true)}>
Open sidebar
</button>
</Sidebar>
);
}
}

export default App;

最佳答案

首先,如果您希望它以关闭状态开始,则将值初始化为 false ..

    this.state = {
sidebarOpen: false
};

其次,如果你想让它打开和关闭,正常设置onClick -> onClick={this.onSetSidebarOpen} :

onSetSidebarOpen() {
this.setState((prevState) => ({ sidebarOpen: !prevState.sidebarOpen }));
}

一般来说 - 当然它会保持打开状态,默认情况下您将该值设置为 true,并且它永远保持 true。

关于javascript - 如何关闭和打开侧边栏 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52099405/

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