Router

createBrowserRouter()

BrowserRouterλ₯Ό μƒμ„±ν•˜λŠ” ν•¨μˆ˜λ‘œ, v6μ—μ„œ μƒˆλ‘­κ²Œ μΆ”κ°€λœ λΌμš°νŒ… 섀계 방법

ν•΄λ‹Ή λΌμš°νŒ… 섀계방식을 μ‚¬μš©ν•˜λ©΄ v6.4에 μΆ”κ°€λœ Data APIsλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

Data APIsλž€?

λΌμš°ν„°κ°€ λ Œλ”λ§ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμ— 데이터λ₯Ό μ „λ‹¬ν•˜λŠ” κΈ°λŠ₯ λ“±, 데이터 λΆˆλŸ¬μ˜€κΈ°μ™€ κ΄€λ ¨λœ μƒˆλ‘œμš΄ κΈ°λŠ₯듀이 λŒ€κ±° μΆ”κ°€ λ˜μ—ˆλ‹€.

function createBrowserRouter(
  routes: RouteObject[],
  opts?: {
    basename?: string;
    future?: FutureConfig;
    hydrationData?: HydrationState;
    window?: Window;
  }
): RemixRouter;
  • routes β†’ 라우트 객체 λ°°μ—΄

  • basename β†’ μ•±μ˜ κΈ°λ³Έ 이름, '/app' 이라고 μ„€μ •μ‹œ '/'λŠ” '/app'을 가리킨닀.

import { createBrowserRouter, RouterProvider } from 'react-router-dom';

const router = createBrowserRouter([
  {
    path: '/',
    element: <Layout />,
    children: [
      { index: true, element: <HomePage /> },
      { path: '/dashboard', element: <DashboardPage /> }
    ]
  }
]);

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

Routes.tsx

ν…ŒμŠ€νŠΈ ν™˜κ²½μ—μ„œλ„ 라우트 정보가 ν•„μš”ν•˜κΈ° λ•Œλ¬Έμ— 라우트 객체 배열을 λ³„λ„μ˜ λͺ¨λ“ˆλ‘œ λΆ„λ¦¬ν•΄μ„œ 관리

routes.tsx

import Layout from './components/Layout';
import HomePage from './pages/HomePage';
import DashboardPage from './pages/DashboardPage';

const routes = [
  {
    path: '/',
    element: <Layout />,
    children: [
      { index: true, element: <HomePage /> },
      { path: '/dashboard', element: <DashboardPage /> }
    ]
  }
];

export default routes;

createRoutesFromElements()

React Element 배열을 라우트 객체 λ°°μ—΄λ‘œ λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜

createBrowserRouter()λ₯Ό μ‚¬μš©ν•  λ•Œ 이전에 μ‚¬μš©λ˜λ˜ JSX ν˜•νƒœλ‘œ λΌμš°ν„°λ₯Ό ꡬ성할 수 μžˆλ‹€.

declare function createRoutesFromElements(
  children: React.ReactNode
): RouteObject[];

interface RouteObject {
  caseSensitive?: boolean;
  children?: RouteObject[];
  element?: React.ReactNode;
  index?: boolean;
  path?: string;
}

Usage

const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path='/' element={<Layout />}>
      <Route index element={<HomePage />} />
      <Route path='/dashbaord' element={<DashboardPage />} />
    </Route>
  )
);

createMemoryRouter()

Test ν™˜κ²½μ—μ„œ createBrowserRouter λŒ€μ‹  createMemoryRouterλ₯Ό μ‚¬μš©ν•œλ‹€.

export declare function createMemoryRouter(routes: RouteObject[], opts?: {
    basename?: string;
    future?: FutureConfig;
    hydrationData?: HydrationState;
    initialEntries?: InitialEntry[];
    initialIndex?: number;
}): RemixRouter;
  • initialEntries β†’ 초기 μ—”νŠΈλ¦¬ λͺ©λ‘ μ„€μ •, 즉 ν…ŒμŠ€νŠΈν•  νŽ˜μ΄μ§€μ˜ 첫 λ Œλ”λ§ 경둜 μ„€μ •

    • 배열인 μ΄μœ λŠ” λΈŒλΌμš°μ € νžˆμŠ€ν† λ¦¬ μŠ€νƒκ³Ό 동일, (뒀둝가기, μ•žμœΌλ‘œκ°€κΈ° ν…ŒμŠ€νŠΈλ₯Ό ν•˜κΈ° μœ„ν•¨)

  • initialIndex β†’ 초기 μ—”νŠΈλ¦¬ λͺ©λ‘[λ°°μ—΄] 쀑 인덱슀λ₯Ό 톡해 경둜λ₯Ό 선택

import { createMemoryRouter, RouterProvider } from 'react-router-dom';

import routes from '../routes';

descirbe('App', () => {
  it('render correctly', () => {
    const router = createMemoryRouter(routes);
    render(<RouterProvider router={router} />)
  )
});

ν•΄λ‹Ή ν•¨μˆ˜λ„ μž¬μ‚¬μš©μ„±μ„ λ†’νžˆκΈ° μœ„ν•΄ ν•¨μˆ˜λ‘œ μΆ”μΆœν•˜μž.

import { createMemoryRouter, RouterProvider } from 'react-router-dom';

import routes from '../routes';

export function withRoutes(initialEntries = '/') {
  const router = createMemoryRouter(routes, {initialEntries: [initialEntries]});
  return <RouterProvider router={router} />;
  )
}

describe('App', () => {
  it('render correctly', () => {
    render(withRoutes());

    expect(screen.getByText(/μ΄λ‹€μž‰/)).toBeInTheDocument();
  });
});

MemoryRouter VS createMemoryRouter()

두 방법 λͺ¨λ‘ ν…ŒμŠ€νŠΈ ν™˜κ²½μ—μ„œ λΌμš°νŒ…μ„ κ΅¬ν˜„ν•˜λŠ”λ° μ‚¬μš©λœλ‹€.

createMemoryRouter()λŠ” μ‹€μ œλ‘œ μ‚¬μš©λ˜λŠ” 전체 λΌμš°νŒ… 정보λ₯Ό κ·ΈλŒ€λ‘œ κ°€μ Έμ™€μ„œ κ΅¬ν˜„ν•˜κΈ° λ•Œλ¬Έμ— 전체적인 λΌμš°νŒ… μƒν˜Έμž‘μš©μ„ ν…ŒμŠ€νŠΈν•  λ•Œ 적합할것 κ°™λ‹€.

반면 MemoryRouterλŠ” 라우트λ₯Ό μ£Όμž… λ°›λŠ” μ‹μœΌλ‘œ μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— μ»€μŠ€ν…€ν•˜κΈ° 쉽고, μ’€ 더 μœ μ—°ν•œ ν…ŒμŠ€νŠΈλ₯Ό μž‘μ„±ν•  수 μžˆμ–΄μ„œ νŠΉμ • λΌμš°νŒ… μ‹œλ‚˜λ¦¬μ˜€λ₯Ό ν…ŒμŠ€νŠΈν•  λ•Œ 적합할 것 κ°™λ‹€.

Last updated