侧边栏不起作用,因为应该

发布于 2025-02-08 10:03:04 字数 3923 浏览 2 评论 0原文

我正在尝试在React应用中创建一个侧栏,但它无法正常工作。

这是我的Navbar组件

import React from "react";
import './Navbar.css';

export default function Navbar({openSideBar}) {

  return (
    <>
      <div className="nav-bar">
        <div className="burger" onClick={openSideBar}>
          <i className="ri-menu-line"></i>
        </div>
        <div className="title">AmProMotors</div>
      </div>
    </>
  );
}

这是我的边栏组件

import React from 'react'
import './SideBar.css'

export default function SideBar(SideBar) {

  return (
    <div className={SideBar?"side-bar-open side-bar" : 'side-bar'}> // Problem is here i think
        <li>Home</li>
        <li>Contact Us</li>
        <li>About Us</li>
        <li>Add New</li>
    </div>
  )
}

这是我的应用程序组件,它具有打开和关闭侧栏的状态

import Singup from "../Authentication/Singup/Singup";
import Dashboar from "../UI/Dashboard";
import Login from "../Authentication/Login/Login";
import { Container } from "react-bootstrap";
import { AuthProvider } from "../../context/AuthContext";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Updateprofile from "../Authentication/UpdateProfile/Updateprofile";
import Dashboard from "../UI/Dashboard";
import PrivateRoute from "../Authentication/PrivateRoute/PrivateRoute";
import Forgotpassword from "../Authentication/ForgotPassword/Forgotpassword";
import Navbar from "../UI/navbar";
import AboutUs from "../UI/About Us";
import ContactUs from "../UI/Contact Us";
import Addnew from "../UI/Addnew";
import { useState } from 'react'
import SideBar from "../UI/Sidebar/SideBar";

function App() {

  const [sideBar, setSideBar] = useState(false);

  function toggleSideBar(){
    setSideBar(true);
    console.log('clicked');
  }

  return (
    <Router>
      <AuthProvider>
        <Navbar openSideBar={toggleSideBar} />
        <SideBar sideBar={sideBar} />
        <Routes>
          <Route exact path="/" element={<Dashboar />} />
          <Route
            path="/update-profile"
            element={
              <PrivateRoute>
                <Updateprofile />
              </PrivateRoute>
            }
          />
          <Route path="/signup" element={<Singup />} />
          <Route path="/login" element={<Login />} />
          <Route path="/forgot-password" element={<Forgotpassword />} />
          <Route path="/update-profile" element={<Updateprofile />} />
          <Route path="/about-us" element={<AboutUs />} />
          <Route path="/contact-us" element={<ContactUs />} />
          <Route path="/add-new" element={<Addnew />} />
        </Routes>
      </AuthProvider>
    </Router>
  );
}

export default App;

我认为这个问题是侧栏组件,但我不确定这是我的侧边栏的CSS

.side-bar{
    position: absolute;
    background-color: black;
    color: white;
    top: 0;
    left: 0;
    height: 100vh;
    width: 280px;
    transform: translateX(-100%);
    transition: all 0.4s ease;
} 

.side-bar li{
    list-style: none;
    padding: 1rem 2rem;
    border-bottom: solid rgba(255, 255, 255) 1px;
    display: flex;
    justify-content: center;
    gap: 1rem;
    font-weight: 300;
}

.side-bar li:hover{
    background-color: whitesmoke;
    color: black;
}

.side-bar-open{
    transform: translateX(0%);
    transition: all 0.4s ease;
}

I am trying to create a side bar in react app but it is not working properly.

This is my navbar component

import React from "react";
import './Navbar.css';

export default function Navbar({openSideBar}) {

  return (
    <>
      <div className="nav-bar">
        <div className="burger" onClick={openSideBar}>
          <i className="ri-menu-line"></i>
        </div>
        <div className="title">AmProMotors</div>
      </div>
    </>
  );
}

This is my side bar component

import React from 'react'
import './SideBar.css'

export default function SideBar(SideBar) {

  return (
    <div className={SideBar?"side-bar-open side-bar" : 'side-bar'}> // Problem is here i think
        <li>Home</li>
        <li>Contact Us</li>
        <li>About Us</li>
        <li>Add New</li>
    </div>
  )
}

This is my app component which has state to open and close side bar

import Singup from "../Authentication/Singup/Singup";
import Dashboar from "../UI/Dashboard";
import Login from "../Authentication/Login/Login";
import { Container } from "react-bootstrap";
import { AuthProvider } from "../../context/AuthContext";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Updateprofile from "../Authentication/UpdateProfile/Updateprofile";
import Dashboard from "../UI/Dashboard";
import PrivateRoute from "../Authentication/PrivateRoute/PrivateRoute";
import Forgotpassword from "../Authentication/ForgotPassword/Forgotpassword";
import Navbar from "../UI/navbar";
import AboutUs from "../UI/About Us";
import ContactUs from "../UI/Contact Us";
import Addnew from "../UI/Addnew";
import { useState } from 'react'
import SideBar from "../UI/Sidebar/SideBar";

function App() {

  const [sideBar, setSideBar] = useState(false);

  function toggleSideBar(){
    setSideBar(true);
    console.log('clicked');
  }

  return (
    <Router>
      <AuthProvider>
        <Navbar openSideBar={toggleSideBar} />
        <SideBar sideBar={sideBar} />
        <Routes>
          <Route exact path="/" element={<Dashboar />} />
          <Route
            path="/update-profile"
            element={
              <PrivateRoute>
                <Updateprofile />
              </PrivateRoute>
            }
          />
          <Route path="/signup" element={<Singup />} />
          <Route path="/login" element={<Login />} />
          <Route path="/forgot-password" element={<Forgotpassword />} />
          <Route path="/update-profile" element={<Updateprofile />} />
          <Route path="/about-us" element={<AboutUs />} />
          <Route path="/contact-us" element={<ContactUs />} />
          <Route path="/add-new" element={<Addnew />} />
        </Routes>
      </AuthProvider>
    </Router>
  );
}

export default App;

I think that problem is with side bar component but i am not sure also this is my css for sidebar if any thing is wrong in it

.side-bar{
    position: absolute;
    background-color: black;
    color: white;
    top: 0;
    left: 0;
    height: 100vh;
    width: 280px;
    transform: translateX(-100%);
    transition: all 0.4s ease;
} 

.side-bar li{
    list-style: none;
    padding: 1rem 2rem;
    border-bottom: solid rgba(255, 255, 255) 1px;
    display: flex;
    justify-content: center;
    gap: 1rem;
    font-weight: 300;
}

.side-bar li:hover{
    background-color: whitesmoke;
    color: black;
}

.side-bar-open{
    transform: translateX(0%);
    transition: all 0.4s ease;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2025-02-15 10:03:04

侧边栏组件中的问题。您需要从道具重组侧栏道具。

import React from 'react'
import './SideBar.css'

export default function SideBar({ sideBar }) { // here is the fix

  return (
    <div className={sideBar ? "side-bar-open side-bar" : 'side-bar'}>
        <li>Home</li>
        <li>Contact Us</li>
        <li>About Us</li>
        <li>Add New</li>
    </div>
  )
}

The problem in in your sidebar component. You need to restructure the sideBar prop from props.

import React from 'react'
import './SideBar.css'

export default function SideBar({ sideBar }) { // here is the fix

  return (
    <div className={sideBar ? "side-bar-open side-bar" : 'side-bar'}>
        <li>Home</li>
        <li>Contact Us</li>
        <li>About Us</li>
        <li>Add New</li>
    </div>
  )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文