英文:
On clicking Sidebar toggle button takes back to default page in React
问题
我有一个使用Hooks创建的简单切换按钮,用于显示/隐藏侧边栏。切换按钮在显示和隐藏侧边栏方面工作得很好。当我点击切换按钮时,它会打开侧边栏。但是,当我点击侧边栏中的任何链接时,链接会将我导航到目标页面。但是现在,当我点击切换按钮以打开侧边栏时,它不再停留在相同的页面,而是导航到默认页面。
这是用户页面:
function User() {
const [isOpen, setIsopen] = useState(false);
const ToggleSidebar = () => {
!isOpen ? setIsopen(true) : setIsopen(false);
}
return (
<div className='userheader'>
<div className='usercontainer'>
<div className='signout-btn-position'>
<SignOut />
</div>
<div className={`sidebar ${isOpen ? 'active' : ''}`}>
<div className="sd-header">
<h4 className="mb-0">Sidebar Header</h4>
</div>
<div className="sd-body">
<ul>
<li>{userName ? <div id='username'><FaUserCircle style={{ marginBottom: '4px', marginRight: '4px' }} />{userName}</div> : "Guest User"}</li>
<i><a href='/user/anesthesiatheory' className='link-btn' >Anesthesia</a></i>
<i><a href='/user/anesthesiatheory' className='link-btn' >Integumentary</a></i>
<i><a href='/user/anesthesiatheory' className='link-btn' >Musculoskeletal</a></i>
<i><a href='/user/anesthesiatheory' className='link-btn' >Cardio & Respiratory</a></i>
</ul>
</div>
</div>
<Link className="btn" onClick={ToggleSidebar}><FaBars /></Link>
<div className={`sidebar-overlay ${isOpen ? 'active' : ''}`} onClick={ToggleSidebar}></div>
</div>
<main>
<Outlet />
</main>
</div>
)
}
export default User
这是我的App.js:
const router = createBrowserRouter(
createRoutesFromElements(
<Route path="/" element={<Home />} >
<Route index element={<About />} />
<Route path="about" element={<About />} />
<Route path="signup" element={<Signup />} />
<Route path="login" element={<Login />} />
<Route path="guestuser" element={<GuestUser />} />
<Route path="User" element={<User />}>
<Route index element={<UserDefault />} />
<Route path="anesthesiatheory" element={<AnesthesiaTheory />} />
<Route path="anesquizmain01" element={<AnesMain01 />} />
<Route path="anesquiz01" element={<AnesQuiz01 />} />
<Route path="anesquizresult01" element={<AnesResult01 />} />
</Route>
</Route>
)
)
function App() {
return (
<div className="App">
<RouterProvider router={router} />
</div>
);
}
export default App;
我希望点击切换按钮时,侧边栏能够显示而不导航到默认页面。请让我知道我做错了什么。期待您宝贵的建议。
英文:
I have a simple toggle using hooks to show/hide sidebar. The toggle button works fine as far as showing and hiding sidebar is concerned. When I click on the toggle button it opens the sidebar. And when I click on any of the link from the side bar, the link takes me to the destined page. But now when I click on the toggle button to open the sidebar, instead of staying on the same page it navigates to the default page.
This is the User page
function User() {
const [isOpen, setIsopen] = useState(false);
const ToggleSidebar = () => {
!isOpen ? setIsopen(true) : setIsopen(false);
}
return (
<div className='userheader'>
<div className='usercontainer'>
<div className='signout-btn-position' >
<SignOut />
</div>
<div className={`sidebar ${isOpen ? 'active' : ''}`}>
<div className="sd-header">
<h4 className="mb-0">Sidebar Header</h4>
</div>
<div className="sd-body">
<ul>
<li>{userName ? <div id='username'><FaUserCircle style={{ marginBottom: '4px', marginRight: '4px' }} />{userName}</div> : "Guest User"}</li>
<i><a href='/user/anesthesiatheory' className='link-btn' >Anesthesia</a></i>
<i><a href='/user/anesthesiatheory' className='link-btn' >Integaumentary</a></i>
<i><a href='/user/anesthesiatheory' className='link-btn' >Musculoskeletal</a></i>
<i><a href='/user/anesthesiatheory' className='link-btn' >Cardio & Respiratory</a></i>
</ul>
</div>
</div>
<Link className="btn" onClick={ToggleSidebar}><FaBars /></Link>
<div className={`sidebar-overlay ${isOpen ? 'active' : ''}`} onClick={ToggleSidebar}></div>
</div>
<main>
<Outlet />
</main>
</div>
)
}
export default User
and this is my App.js
const router = createBrowserRouter(
createRoutesFromElements(
<Route path="/" element={<Home />} >
<Route index element={<About />} />
<Route path="about" element={<About />} />
<Route path="signup" element={<Signup />} />
<Route path="login" element={<Login />} />
<Route path="guestuser" element={<GuestUser />} />
<Route path="User" element={<User />}>
<Route index element={<UserDefault />} />
<Route path="anesthesiatheory" element={<AnesthesiaTheory />} />
<Route path="anesquizmain01" element={<AnesMain01 />} />
<Route path="anesquiz01" element={<AnesQuiz01 />} />
<Route path="anesquizresult01" element={<AnesResult01 />} />
</Route>
</Route>
)
)
function App() {
return (
<div className="App">
<RouterProvider router={router} />
</div>
);
}
export default App;
What I want is that when I click on the toggle button, I want the sidebar to show without navigating me to default page. Kindly let me know what I am doing wrong. Looking forwards for your valuable suggestions.
答案1
得分: 0
在这里你正在使用
<Link className="btn" onClick={ToggleSidebar}><FaBars /></Link>
它会重定向到默认页面,使用 button
代替 <Link>
来解决:
<button className="btn" onClick={ToggleSidebar}><FaBars /></button>
另外,清理你的 ToggleSidebar
函数,从:
const ToggleSidebar = () => {
!isOpen ? setIsopen(true) : setIsopen(false);
}
改为:
const ToggleSidebar = () => {
setIsopen(!isOpen)
}
英文:
here you are using
<Link className="btn" onClick={ToggleSidebar}><FaBars /></Link>
which is redirecting to default page,
use button
instead of <Link>
to solve this;
<button className="btn" onClick={ToggleSidebar}><FaBars /></button>
also clean your function for ToggleSidebar
from
const ToggleSidebar = () => {
!isOpen ? setIsopen(true) : setIsopen(false);
}
to
const ToggleSidebar = () => {
setIsopen(!isOpen)
}
答案2
得分: 0
尝试将您的切换按钮从<Link>
元素更改为<button>
元素。来自react-router-dom的<Link>
元素充当锚<a>
(如果您在文档中检查它,它会显示为锚),并且如果未设置href=""
属性,它将重定向到/
或刷新页面。因此,请在Users页面中将该行更改为:
<button className="btn" onClick={ToggleSidebar}><FaBars /></button>
PS 如果您坚持要保留<Link>
标签,那么可以尝试在ToggleSidebar
函数中阻止默认操作:
const ToggleSidebar = (event) => {
event.preventDefaul()
!isOpen ? setIsopen(true) : setIsopen(false);
}
然后在<Link>
元素中:
<Link className="btn" onClick={ (event) => ToggleSidebar(event) }><FaBars /></Link>
请注意,我以前没有尝试过在<Link>
或<a>
标签上使用此方法,我不确定是否有效,但<button>
修复方法应该可行。
希望这有所帮助,如果需要更多帮助,请告诉我
英文:
Try changing your toggle button from a <Link>
element to maybe a <button>
element. The <Link>
element from react-router-dom acts as an anchor <a>
(if you inspect it on your document, it displays as an anchor), and without the href=""
property set, it will just redirect you to /
or refresh the page. So change that line in the Users page as so:
<button className="btn" onClick={ToggleSidebar}><FaBars /></button>
PS
if you insist on keeping the <Link>
tag, then maybe try preventing the default action in your ToggleSidebar
function:
const ToggleSidebar = (event) => {
event.preventDefaul()
!isOpen ? setIsopen(true) : setIsopen(false);
}
then in the <Link>
element:
<Link className="btn" onClick={ (event) => ToggleSidebar(event) }><FaBars /></Link>
Note that I haven't tried this on a <Link>
or <a>
tag in the past, and I am not sure if this will work, but the <button> fix ought to do it.
Hope this was helpful, let me know if you need any more help
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论