【问题标题】:Difference between declaring Routes in React Router v6 and v6.4+在 React Router v6 和 v6.4+ 中声明路由的区别
【发布时间】:2023-02-25 05:15:00
【问题描述】:

我试过搜索,但是,无法真正找到使用 React Fragments 是否可以实现与 pre-v6.4 相同的行为?

const Routes6 = (
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="posts" element={<Posts />}>
      <Route path="/posts/new" element={<NewPost />} />
    </Route>
  </Routes>
);
const routes64 = createBrowserRouter(
  createRoutesFromElements(
    <>
      <Route path="/" element={<Home />} />
      <Route path="/posts" element={<Posts />}>
        <Route path="/posts/new" element={<NewPost />} />
      </Route>
    </>,
  ),
);

【问题讨论】:

    标签: javascript reactjs react-router react-router-dom


    【解决方案1】:

    我看不出为什么使用 React.Fragements 无法使用新的 RRDv6.4+ 数据路由器来实现路由。一种更传统的避免 React.Fragment 的方法可能是在 "/" 上使用根路由,并在索引路由上渲染 Home 组件。

    例子:

    const routes64 = createBrowserRouter(
      createRoutesFromElements(
        <Route path="/">
          <Route index element={<Home />} />
          <Route path="/posts" element={<Posts />}>
            <Route path="new" element={<NewPost />} />
          </Route>
        </Route>,
      ),
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 2021-11-03
      • 2022-07-09
      • 2022-08-14
      • 2022-08-02
      相关资源
      最近更新 更多