Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

redirecting to a different route if user is logged in

<Route path='/profile' element={<PrivateRoute/>}>
  <Route path='/profile' element={<Home/>}/> // inner/nested route
</Route>


// Your PrivateRoute Component should look like this
import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import { auth } from '*somewhere*';

const PrivateRoute = () => {
  const { userLoggedIn } = auth();
  if (userLoggedIn) {
    return <Outlet />; // i.e continue to the inner/nested route
  } else {
    return <Navigate to="/login" />;
  }
  
  // same thing as 
  // 'return userLoggedIn ? <Outlet /> : <Navigate to="/login" />;'
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: discord js duplicate channel 
Javascript :: Javascript prime number check 
Javascript :: docker react module not found 
Javascript :: xml to json api in asp.net 
Javascript :: convert functoin with call back to promise 
Javascript :: document get element by id style 
Javascript :: how to check if connected to internet js 
Javascript :: display am pm in javascript 
Javascript :: react native regenerate android and ios folders 
Javascript :: react overflow scroll 
Javascript :: react redux wait for props 
Javascript :: sum two objects javascript 
Javascript :: howt to disable a select tag using js 
Javascript :: how to create a button with react 
Javascript :: javascript object first key 
Javascript :: how to get relative postiion mouse click on element 
Javascript :: (node:2496) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. 
Javascript :: react native run android 
Javascript :: form submit programmatically 
Javascript :: return first letter of string javascript in uppercase 
Javascript :: jquery selected option value 
Javascript :: jquery datetimepicker example code 
Javascript :: imagebackground in react native 
Javascript :: js get part of array 
Javascript :: jquery $(...)..each() is not a function 
Javascript :: rounding off in javascript 
Javascript :: how get one value of array of object in javascript 
Javascript :: FileReader get filename 
Javascript :: javascript string pop 
Javascript :: jquery this 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =