Jak uzyskać trasy z innej lokalizacji/funkcje w react-router-dom v6?

0

Pytanie

Staram się reagować na router V06, ale zetknął się z problemem, gdy próbowałem zrobić to poniżej, ale wszystkie trasy i strony są puste.

Kod:

// Main App Function
    const App = () => {
      return (
        <Router>
          <Fragment>
            <Navbar />
            <Routes>
              <Route index strict path="/" element={<Landing />} />
      **{/* Here I want to render paths from other function*/}**
              <Route element={<AppRoutes />} />
            </Routes>
          </Fragment>
        </Router>
      );
    };
    
    export default App;
    
    // In AppRoutes Function
    const AppRoutes = () => (
      <section className="container">
        <Routes>
          <Route strict path="login" element={<Login />} />
          <Route strict path="register" element={<Register />} />
          <Route path="*" element={<NotFound />} />
        </Routes>
      </section>
    );
    
    export default AppRoutes;
1

Najlepsza odpowiedź

1

W programie należy zadeklarować Landing składnik, który będzie stroną indeksu i wizualizacja AppRoutes po drodze"/*", aby umożliwić dopasowanie подмаршрутов.

App

<Router>
  <Navbar />
  <Routes>
    <Route index element={<Landing />} />
    <Route path="/*" element={<AppRoutes />} />
  </Routes>
</Router>

Aplikacje

const AppRoutes = () => (
  <section className="container">
    <Routes>
      <Route path="login" element={<Login />} />
      <Route path="register" element={<Register />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  </section>
);

Edit how-to-get-routes-from-other-location-function-in-react-router-dom-v6

2021-11-24 00:18:18

Dziękuję, głupie z mojej strony, nie mogłem rozwiązać ten mały problem. To działa!
Gazi

@Gazi Nie martw się, z nami wszystkimi czasami to się zdarza. Nie zapominaj o tym, co zrobić, gdy ktoś odpowie. Hura!
Drew Reese

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................