Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

useRoutes

// React Router v6:
// hook functionally equivalent to <Routes>, but uses JS objects instead
// of <Route>, use this hook instead of 'react-router-config' (React Router v5)
import { useRoutes } from 'react-router-dom';

function App() {
  let element = useRoutes([
    {
      path: '/',
      element: <Dash />,
      children: [
        { path: 'messages', element: <DashMessages /> },
        { path: 'tasks', element: <DashTasks /> }
      ]
    },
    { path: 'team', element: <About /> }
  ]);

  return element;
}
Comment

useroutes how to use

//routes.js

import { useRoutes } from "react-router-dom";
import Home from './pages/Home'
import About from './pages/About'
import Login from './pages/Login'
import SignUp from './pages/Signup'
import AuthLayout from './layouts/AuthLayout'
import MainLayout from './layouts/MainLayout'

export default function Router() {
  let element = useRoutes([
      {
          element: <AuthLayout />,
          children: [
             { path: "/", element: <Login /> },
             { path: "signup", element: <SignUp /> },
          ],
      },
      {
          element: <MainLayout />,
          children: [
              { path: "home", element: <Home /> },
              { path: "about", element: <About /> },
          ],
      },
  ]);
  return element;
}

//App.js

import "./styles.css";
import { BrowserRouter } from "react-router-dom";
import Router from './routes'

export default function App() {
  return (
      <BrowserRouter>
          <div className="App">
              <h1>useRoutes Example</h1>
              <Router/>
          </div>
      </BrowserRouter>
  );
}

//link to full example
//https://codesandbox.io/s/thirsty-architecture-jgok5?file=/src/App.js
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs send download file from buffer 
Javascript :: javascript select audio device 
Javascript :: how to get data from for loop in react native 
Javascript :: view child with directive not working undefined 
Javascript :: sort object properties by value javascript 
Javascript :: javascript export default 
Javascript :: google scripts get document 
Javascript :: how to check provided value is in array in javascript 
Javascript :: javascript check if it has passed midnight 
Javascript :: how to calculate time taken for ajax call in javascript 
Javascript :: javascript check undefined or null 
Javascript :: react hook usestate 
Javascript :: find second smallest number in array 
Javascript :: spawn prop with custom model 
Javascript :: node_modules/metro/src/lib/attachWebsocketServer.js 
Javascript :: indexof all occurrences javascript 
Javascript :: lunix increae ram available to nodejs 
Javascript :: color picker in react js 
Javascript :: mongoose autoincrement 
Javascript :: https://jsonplaceholder.typicode.com/albums/1 
Javascript :: js insert a point each three digit 
Javascript :: jq click with trigger load data 
Javascript :: javascript scroll to element with offset 
Javascript :: save data response from fetch as global param js 
Javascript :: useWidthSize 
Javascript :: regex more than one character 
Javascript :: how to format a javascript date 
Javascript :: onclick automatically called after 10 seconds 
Javascript :: invert linked list js 
Javascript :: shopify guest login 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =