Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React PrivateRoute componenet

import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';

const PrivateRoute = () => {
    const auth = null; // determine if authorized, from context or however you're doing it

    // If authorized, return an outlet that will render child elements
    // If not, return element that will navigate to login page
    return auth ? <Outlet /> : <Navigate to="/login" />;
}


 // ###########################
import './App.css';
import React, {Fragment} from 'react';
import {BrowserRouter as Router, Route, Routes} from 'react-router-dom';
import Navbar from './components/layout/Navbar';
import Home from './components/pages/Home';
import Register from './components/auth/Register'
import Login from './components/auth/Login';
import PrivateRoute from './components/routing/PrivateRoute';

const App = () => {
  return (
    <Router>
      <Fragment>
        <Navbar/>
        <Routes>
          <Route exact path='/' element={<PrivateRoute/>}>
            <Route exact path='/' element={<Home/>}/>
          </Route>
          <Route exact path='/register' element={<Register/>}/>
          <Route exact path='/login' element={<Login/>}/>
        </Routes>
      </Fragment>
    </Router>
    
  );
}


// ###############
<Route exact path='/' element={<PrivateRoute/>}>
      <Route exact path='/' element={<Home/>}/>
</Route>
Comment

PREVIOUS NEXT
Code Example
Javascript :: check if a specific user is banned discord js 
Javascript :: convert js to tsx 
Javascript :: dropzone js change dynamic url 
Javascript :: hsv to rgb js 
Javascript :: toast.toastAlert ext js 
Javascript :: what is prototype-based in javascreipt 
Javascript :: what happens if pass argument to a function that does not have parameters javascript 
Javascript :: Component With Both Data And Props 
Javascript :: mongoose lookup array of objects 
Javascript :: regex remove whitespace 
Javascript :: javascript enum includes value 
Javascript :: fetching coordinates from db to map 
Javascript :: vite displays blank page in docker container 
Javascript :: puppeteer click is not working 
Javascript :: nodejs split array into chunks 
Javascript :: sort array without using sort function in javascript 
Javascript :: window handles 
Javascript :: delete file firebase angular 
Javascript :: javascript powerpoint 
Javascript :: load content on user language in javascript 
Javascript :: react native pass params to previous screen 
Javascript :: copy one cell value to another in google app script 
Javascript :: angular resolver with parameters 
Javascript :: FILTER METHOD. IMPORTANT 
Javascript :: hide navbar and footer on certain pages like dashboard. react-router 
Javascript :: How to append variable with anchor element href link in Angularjs 
Javascript :: Angularjs onchange datetime picker not working 
Javascript :: React Native : Add a band of color in the background 
Javascript :: javascript array add method 
Javascript :: express and jade, ignore render errors 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =