iT邦幫忙

2023 iThome 鐵人賽

DAY 20
0

由於在app.jsx 不能使用useLocation 請他生成Navbar元件要使用useLocation更新點擊時按鍵顏色

咒語:app.jsx不能用useLocation 要怎樣增加跟引入Navbar 請給我細節

在你的項目中創建一個名為 Navbar.jsx 的新組件,該組件將用於顯示導航欄

import React from "react";
import { Link, useLocation } from "react-router-dom";
import styled from "styled-components";

const NavWrapper = styled.nav`
  ul {
    list-style-type: none;
    padding: 0;
    display: flex;
    gap: 20px;

    li {
      a {
        text-decoration: none;
        color: white;
      }
    }
  }
`;

function Navbar() {
  const location = useLocation();
  const currentPath = location.pathname;

  return (
       <NavWrapper>
      <ul>
        <li
          style={{
            backgroundColor: pathname === "/" ? "#ccc" : "transparent",
            color: pathname === "/" ? "black" : "white",
          }}
        >
          <Link to="/">首頁</Link>
        </li>
        <li
          style={{
            backgroundColor:
              pathname === "/member" || pathname === "/login"
                ? "#ccc"
                : "transparent",
            color: pathname === "/member" ? "black" : "white",
          }}
        >
          <Link to={AC.token === "1" ? "/member" : "/login"}>
            {AC.token === "1" ? "會員" : "登入"}
          </Link>
        </li>
        <li
          style={{
            backgroundColor: pathname === "/product" ? "#ccc" : "transparent",
            color: pathname === "/product" ? "black" : "white",
          }}
        >
          <Link to="/product">商品頁</Link>
        </li>
        <li
          style={{
            backgroundColor: pathname === "/cart" ? "#ccc" : "transparent",
            color: pathname === "/cart" ? "black" : "white",
          }}
        >
          <Link to="/cart">購物車</Link>
        </li>
        <li
          style={{
            backgroundColor: pathname === "/ChatRoom" ? "#ccc" : "transparent",
            color: pathname === "/ChatRoom" ? "black" : "white",
          }}
        >
          <Link to="/ChatRoom">聊天室</Link>
        </li>
        <li
          style={{
            backgroundColor:
              pathname === "/MessageBoard" ? "#ccc" : "transparent",
            color: pathname === "/MessageBoard" ? "black" : "white",
          }}
        >
          <Link to="/MessageBoard">留言板</Link>
        </li>
        <li
          style={{
            backgroundColor:
              pathname === "/MusicPlayer" ? "#ccc" : "transparent",
            color: pathname === "/MusicPlayer" ? "black" : "white",
          }}
        >
          <Link to="/MusicPlayer">音樂播放器</Link>
        </li>
        <li
          style={{
            backgroundColor:
              pathname === "/WeatherApp" ? "#ccc" : "transparent",
            color: pathname === "/WeatherApp" ? "black" : "white",
          }}
        >
          <Link to="/WeatherApp">天氣應用</Link>
        </li>
      </ul>
    </NavWrapper>
  );
}

export default Navbar;

步驟 2:在 App.jsx 中引入導航欄

import React, { useState } from "react";
import {
  BrowserRouter as Router,
  Route,
  Routes,
  Link,
} from "react-router-dom";
import { AppBar, Toolbar, Typography, Container } from "@mui/material";
import styled from "styled-components";
import Home from "./pages/home";
import Login from "./pages/login";
import Member from "./pages/member";
import Product from "./pages/product";
import Cart from "./pages/cart";
import ChatRoom from "./pages/chat";
import MessageBoard from "./pages/messageboard";
import MusicPlayer from "./pages/MusicPlayer";
import WeatherApp from "./pages/WeatherApp";
import UserProfile from "./pages/UserProfile";
import Survey from "./pages/Survey";
import Navbar from "./Navbar"; // 引入导航栏组件

const AppWrapper = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 20px;
`;

// ...(省略了其他部分)

function App() {
  const [token, setToken] = useState(localStorage.getItem("token"));
  const [cart, setCart] = useState([]);

  return (
    <AllContext.Provider value={{ cart, setCart, token, setToken }}>
      {" "}
      {/* 使用 CartProvider 包裹整個應用 */}
      <Router>
        <AppBar position="static">
          <Toolbar>
            <Typography variant="h6" mr={2}>
              鐵人賽
            </Typography>
            <Navbar />
          </Toolbar>
        </AppBar>
        <Container>
          <AppWrapper>
            <Routes>
              {/* ...(省略了其他部分) */}
            </Routes>
          </AppWrapper>
        </Container>
      </Router>
    </AllContext.Provider>
  );
}

export default App;

這樣就可以根據上頭的path 改變link的顏色了


上一篇
[Day19] 個人資訊頁面
下一篇
[Day21] 要放假了 今晚吃什麼
系列文
I have an AI ,I have a React, Ugh ,AI -React 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言