Come arrivare itinerari da un'altra posizione/funzione di reagire-router-dom v6?

0

Domanda

Sto cercando di Reagire Router V06, ma di fronte a un problema in cui ho cercato di seguito in questo modo, ma tutti i percorsi e pagina sono vuoti.

Codice:

// 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

Migliore risposta

1

In app, dichiarare la Landing componente la pagina di indice, e render AppRoutes su un "/*" percorso per consentire corrispondente sub-rotte.

App

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

AppRoutes

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

Grazie, sciocco di me, non riuscivo a capire questo piccolo problema. Funziona alla grande!
Gazi

@Gazi Non preoccuparti, succede a tutti noi, a volte. Non dimenticare che cosa fare quando qualcuno risponde. Evviva!
Drew Reese

In altre lingue

Questa pagina è in altre lingue

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