Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] react-router link 버그 #14

Closed
flareseek opened this issue Sep 13, 2024 · 4 comments
Closed

[fix] react-router link 버그 #14

flareseek opened this issue Sep 13, 2024 · 4 comments
Assignees
Labels
bug 제대로 작동하지 않아요

Comments

@flareseek
Copy link
Owner

flareseek commented Sep 13, 2024

버그 설명

react-router의 Link가 작동하지 않음

버그 발생 경로

App.tsx에 Link 태그 생성해서 테스트 중에 발견했습니다.

원인은 RouterProvider가 root에 있어야 됩니다.

기타사항

RouterPath 부분에

{
    path: "",
    element: <App />,
  }

로 두고

App.tsx 안에
<Main /> 을 호출하는 식으로 해결해야 합니다.

@flareseek flareseek added the bug 제대로 작동하지 않아요 label Sep 13, 2024
@flareseek flareseek self-assigned this Sep 13, 2024
@jjh4450
Copy link
Collaborator

jjh4450 commented Sep 13, 2024

헤더 만들면서 라우터 객체를 분리해 관리할 예정입니다.
RouterInfo.ts

import Layout from "./components/Layout/index.tsx";
import Main from "./pages/Main/index.tsx";
import Artist from "./pages/Artist/index.tsx";
import Booth from "./pages/Booth/index.tsx";
import BoothNFoodList from "./pages/BoothNFoodList/index.tsx";
import Foodtruck from "./pages/Foodtruck/index.tsx";
import Makers from "./pages/Makers/index.tsx";
import Map from "./pages/Map/index.tsx";
import Notice from "./pages/Notice/index.tsx";
import QnA from "./pages/QnA/index.tsx";
import Timetable from "./pages/Timetable/index.tsx";

export const RouterInfo = [
    {
        path: "",
        element: <Main />,
        english: "Main",
        korean: "메인",
        expose: true,
    },
    {
        path: "artist/:id",
        element: <Artist />,
        english: "Artist",
        korean: "아티스트",
        expose: false,
    },
    {
        path: "booth/:id",
        element: <Booth />,
        english: "Booth",
        korean: "부스",
        expose: false,
    },
    {
        path: "booth_foodtruck_list",
        element: <BoothNFoodList />,
        english: "Booth & Foodtruck List",
        korean: "부스 & 푸드트럭 리스트",
        expose: true,
    },
    {
        path: "foodtruck/:id",
        element: <Foodtruck />,
        english: "Foodtruck",
        korean: "푸드트럭",
        expose: false,
    },
    {
        path: "makers",
        element: <Makers />,
        english: "Makers",
        korean: "메이커스",
        expose: false,
    },
    {
        path: "map",
        element: <Map />,
        english: "Map",
        korean: "지도",
        expose: true,
    },
    {
        path: "notice",
        element: <Notice />,
        english: "Notice",
        korean: "공지사항",
        expose: true,
    },
    {
        path: "QnA",
        element: <QnA />,
        english: "QnA",
        korean: "QnA",
        expose: true,
    },
    {
        path: "timetable",
        element: <Timetable />,
        english: "Timetable",
        korean: "타임테이블",
        expose: true,
    },
];

export default RouterInfo;

App.tsx

import { createBrowserRouter, RouterProvider } from "react-router-dom";
import Layout from "./components/Layout";
import RouterInfo from "./RouterInfo.tsx";


const RouterPath = RouterInfo.map((info) => {
    return {
        path: info.path,
        element: info.element,
    }
});

const router = createBrowserRouter([
  {
    path: "/",
    element: <Layout />,
    children: RouterPath,
  },
]);

function App() {
  return <RouterProvider router={router} />;
}

export default App;

고려 해야 할 부분이 있을까요?

@flareseek
Copy link
Owner Author

헤더 만들면서 라우터 객체를 분리해 관리할 예정입니다. RouterInfo.ts

import Layout from "./components/Layout/index.tsx";
import Main from "./pages/Main/index.tsx";
import Artist from "./pages/Artist/index.tsx";
import Booth from "./pages/Booth/index.tsx";
import BoothNFoodList from "./pages/BoothNFoodList/index.tsx";
import Foodtruck from "./pages/Foodtruck/index.tsx";
import Makers from "./pages/Makers/index.tsx";
import Map from "./pages/Map/index.tsx";
import Notice from "./pages/Notice/index.tsx";
import QnA from "./pages/QnA/index.tsx";
import Timetable from "./pages/Timetable/index.tsx";

export const RouterInfo = [
    {
        path: "",
        element: <Main />,
        english: "Main",
        korean: "메인",
        expose: true,
    },
    {
        path: "artist/:id",
        element: <Artist />,
        english: "Artist",
        korean: "아티스트",
        expose: false,
    },
    {
        path: "booth/:id",
        element: <Booth />,
        english: "Booth",
        korean: "부스",
        expose: false,
    },
    {
        path: "booth_foodtruck_list",
        element: <BoothNFoodList />,
        english: "Booth & Foodtruck List",
        korean: "부스 & 푸드트럭 리스트",
        expose: true,
    },
    {
        path: "foodtruck/:id",
        element: <Foodtruck />,
        english: "Foodtruck",
        korean: "푸드트럭",
        expose: false,
    },
    {
        path: "makers",
        element: <Makers />,
        english: "Makers",
        korean: "메이커스",
        expose: false,
    },
    {
        path: "map",
        element: <Map />,
        english: "Map",
        korean: "지도",
        expose: true,
    },
    {
        path: "notice",
        element: <Notice />,
        english: "Notice",
        korean: "공지사항",
        expose: true,
    },
    {
        path: "QnA",
        element: <QnA />,
        english: "QnA",
        korean: "QnA",
        expose: true,
    },
    {
        path: "timetable",
        element: <Timetable />,
        english: "Timetable",
        korean: "타임테이블",
        expose: true,
    },
];

export default RouterInfo;

App.tsx

import { createBrowserRouter, RouterProvider } from "react-router-dom";
import Layout from "./components/Layout";
import RouterInfo from "./RouterInfo.tsx";


const RouterPath = RouterInfo.map((info) => {
    return {
        path: info.path,
        element: info.element,
    }
});

const router = createBrowserRouter([
  {
    path: "/",
    element: <Layout />,
    children: RouterPath,
  },
]);

function App() {
  return <RouterProvider router={router} />;
}

export default App;

고려 해야 할 부분이 있을까요?

오 좋네요. routerInfo와 routerPath도 타입관리 해주면 더 좋겠네요

아 그리고 수정사항 하나 더 있는데 router경로관련 변수명은 jsx와 헷갈리지 않게 camelCase로 작성해주세요

@jjh4450
Copy link
Collaborator

jjh4450 commented Sep 13, 2024

아 아직 작성 중이라 미쳐 신경 쓰지 못했는데 pr 전에 참고하여 수정하겠습니다.

@flareseek
Copy link
Owner Author

#18 에서 해결완료

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 제대로 작동하지 않아요
Projects
None yet
Development

No branches or pull requests

2 participants