点击侧边栏切换按钮会回到React中的默认页面。

huangapple go评论99阅读模式
英文:

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 = () =&gt; {
!isOpen ? setIsopen(true) : setIsopen(false);
}
return (
&lt;div className=&#39;userheader&#39;&gt;
&lt;div className=&#39;usercontainer&#39;&gt;
&lt;div className=&#39;signout-btn-position&#39; &gt;
&lt;SignOut /&gt;
&lt;/div&gt;
&lt;div className={`sidebar ${isOpen ? &#39;active&#39; : &#39;&#39;}`}&gt;
&lt;div className=&quot;sd-header&quot;&gt;
&lt;h4 className=&quot;mb-0&quot;&gt;Sidebar Header&lt;/h4&gt;
&lt;/div&gt;
&lt;div className=&quot;sd-body&quot;&gt;
&lt;ul&gt;
&lt;li&gt;{userName ? &lt;div id=&#39;username&#39;&gt;&lt;FaUserCircle style={{ marginBottom: &#39;4px&#39;, marginRight: &#39;4px&#39; }} /&gt;{userName}&lt;/div&gt; : &quot;Guest User&quot;}&lt;/li&gt;
&lt;i&gt;&lt;a href=&#39;/user/anesthesiatheory&#39; className=&#39;link-btn&#39; &gt;Anesthesia&lt;/a&gt;&lt;/i&gt;
&lt;i&gt;&lt;a href=&#39;/user/anesthesiatheory&#39; className=&#39;link-btn&#39; &gt;Integaumentary&lt;/a&gt;&lt;/i&gt;
&lt;i&gt;&lt;a href=&#39;/user/anesthesiatheory&#39; className=&#39;link-btn&#39; &gt;Musculoskeletal&lt;/a&gt;&lt;/i&gt;
&lt;i&gt;&lt;a href=&#39;/user/anesthesiatheory&#39; className=&#39;link-btn&#39; &gt;Cardio &amp; Respiratory&lt;/a&gt;&lt;/i&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;Link className=&quot;btn&quot; onClick={ToggleSidebar}&gt;&lt;FaBars /&gt;&lt;/Link&gt;
&lt;div className={`sidebar-overlay ${isOpen ? &#39;active&#39; : &#39;&#39;}`} onClick={ToggleSidebar}&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;main&gt;
&lt;Outlet /&gt;
&lt;/main&gt;
&lt;/div&gt;
)
}
export default User

and this is my App.js


const router = createBrowserRouter(
createRoutesFromElements(
&lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} &gt;
&lt;Route index element={&lt;About /&gt;} /&gt;
&lt;Route path=&quot;about&quot; element={&lt;About /&gt;} /&gt;
&lt;Route path=&quot;signup&quot; element={&lt;Signup /&gt;} /&gt;
&lt;Route path=&quot;login&quot; element={&lt;Login /&gt;} /&gt;
&lt;Route path=&quot;guestuser&quot; element={&lt;GuestUser /&gt;} /&gt;
&lt;Route path=&quot;User&quot; element={&lt;User /&gt;}&gt;
&lt;Route index element={&lt;UserDefault /&gt;} /&gt;
&lt;Route path=&quot;anesthesiatheory&quot; element={&lt;AnesthesiaTheory /&gt;} /&gt;
&lt;Route path=&quot;anesquizmain01&quot; element={&lt;AnesMain01 /&gt;} /&gt;
&lt;Route path=&quot;anesquiz01&quot; element={&lt;AnesQuiz01 /&gt;} /&gt;
&lt;Route path=&quot;anesquizresult01&quot; element={&lt;AnesResult01 /&gt;} /&gt;
&lt;/Route&gt;
&lt;/Route&gt;
)
)
function App() {
return (
&lt;div className=&quot;App&quot;&gt;
&lt;RouterProvider router={router} /&gt;
&lt;/div&gt;
);
}
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

在这里你正在使用

&lt;Link className=&quot;btn&quot; onClick={ToggleSidebar}&gt;&lt;FaBars /&gt;&lt;/Link&gt;

它会重定向到默认页面,使用 button 代替 &lt;Link&gt; 来解决:

<button className=&quot;btn&quot; onClick={ToggleSidebar}&gt;&lt;FaBars /&gt;&lt;/button&gt;

另外,清理你的 ToggleSidebar 函数,从:

const ToggleSidebar = () =&gt; {
  !isOpen ? setIsopen(true) : setIsopen(false);
}

改为:

const ToggleSidebar = () =&gt; {
  setIsopen(!isOpen)
}
英文:

here you are using

&lt;Link className=&quot;btn&quot; onClick={ToggleSidebar}&gt;&lt;FaBars /&gt;&lt;/Link&gt;

which is redirecting to default page,
use button instead of &lt;Link&gt; to solve this;

&lt;button className=&quot;btn&quot; onClick={ToggleSidebar}&gt;&lt;FaBars /&gt;&lt;/button&gt;

also clean your function for ToggleSidebar

from

  const ToggleSidebar = () =&gt; {
!isOpen ? setIsopen(true) : setIsopen(false);
}

to

const ToggleSidebar = () =&gt; {
setIsopen(!isOpen)
}

答案2

得分: 0

尝试将您的切换按钮从&lt;Link&gt;元素更改为&lt;button&gt;元素。来自react-router-dom的&lt;Link&gt;元素充当锚&lt;a&gt;(如果您在文档中检查它,它会显示为锚),并且如果未设置href=&quot;&quot;属性,它将重定向到/或刷新页面。因此,请在Users页面中将该行更改为:

&lt;button className=&quot;btn&quot; onClick={ToggleSidebar}&gt;&lt;FaBars /&gt;&lt;/button&gt;

PS 如果您坚持要保留&lt;Link&gt;标签,那么可以尝试在ToggleSidebar函数中阻止默认操作:

const ToggleSidebar = (event) =&gt; {
  event.preventDefaul()
  !isOpen ? setIsopen(true) : setIsopen(false);
}

然后在&lt;Link&gt;元素中:

&lt;Link className=&quot;btn&quot; onClick={ (event) =&gt; ToggleSidebar(event) }&gt;&lt;FaBars /&gt;&lt;/Link&gt;

请注意,我以前没有尝试过在&lt;Link&gt;&lt;a&gt;标签上使用此方法,我不确定是否有效,但&lt;button&gt;修复方法应该可行。

希望这有所帮助,如果需要更多帮助,请告诉我 点击侧边栏切换按钮会回到React中的默认页面。

英文:

Try changing your toggle button from a &lt;Link&gt; element to maybe a &lt;button&gt; element. The &lt;Link&gt; element from react-router-dom acts as an anchor &lt;a&gt; (if you inspect it on your document, it displays as an anchor), and without the href=&quot;&quot; property set, it will just redirect you to / or refresh the page. So change that line in the Users page as so:

&lt;button className=&quot;btn&quot; onClick={ToggleSidebar}&gt;&lt;FaBars /&gt;&lt;/button&gt;

PS
if you insist on keeping the &lt;Link&gt; tag, then maybe try preventing the default action in your ToggleSidebar function:

  const ToggleSidebar = (event) =&gt; {
event.preventDefaul()
!isOpen ? setIsopen(true) : setIsopen(false);
}

then in the &lt;Link&gt; element:

&lt;Link className=&quot;btn&quot; onClick={ (event) =&gt; ToggleSidebar(event) }&gt;&lt;FaBars /&gt;&lt;/Link&gt;

Note that I haven't tried this on a &lt;Link&gt; or &lt;a&gt; 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 点击侧边栏切换按钮会回到React中的默认页面。

huangapple
  • 本文由 发表于 2023年6月13日 14:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定