Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

protected route in react router 6

Private Route--------

import { Navigate,useLocation} from "react-router-dom"
function Protecte({auth, children }) {   
    return auth ? children : <Navigate to="/login" />;
   
}
export default Protecte
-----------------------------------

App.js---
<Route path="/admin" element={
            <Protecte auth={isLoggedIn}>
              <Admin />
            </Protecte>
    }
   />
Comment

protected route in react js

import React from 'react'
import { Redirect, Route } from 'react-router-dom'

const Protected = ({ component, ...rest }) => {
	<Route
		{...rest}
		render={() => {
			localStorage.getItem('login') ? (
				<component {...props} />
			) : (
				<Redirect to='/login' />
			)
		}}
	/>
};
export default Protected


// app.js 
import Protected from'./components/Protected'

<Router>
<Protected exact path="/"component={Home}/>
<Protected exact path="/about"component={About}/>
<Protected exact path="/contact"component={Contact}/>
</Router>
Comment

react protected route

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

const PrivateRoutes = () => {
  const location = useLocation();
  const { authLogin } = useContext(globalC);
  console.log("authLogin", authLogin);

  return authLogin 
    ? <Outlet />
    : <Navigate to="/login" replace state={{ from: location }} />;
}
Comment

react protected route

<BrowserRouter>
  <Routes>
    <Route path="/" element={<PrivateRoutes />} >
      <Route path="/dashboard" element={<Dashboard />} />
      <Route path="/about" element={<About />} />
    </Route>
    <Route path="/login" element={<Login />} />
    <Route path="*" element={<PageNotFound />} />
  </Routes>
</BrowserRouter>
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery set hidden field value 
Javascript :: nodejs reverse string 
Javascript :: js get url variables 
Javascript :: react text input onchange 
Javascript :: compare two dates in javascript yyyy-mm-dd 
Javascript :: javascript indexof 
Javascript :: set a value in session using javascript 
Javascript :: javascript loop over the alphabet and return the vowels 
Javascript :: express js npm 
Javascript :: how to run function after animation complete jquery 
Javascript :: how to target child element of an event object in JS 
Javascript :: convert to 24 hours format javasript 
Javascript :: send serialized form data jquery 
Javascript :: http to https express js 
Javascript :: javascript Using debugger 
Javascript :: bootstrap datepicker options 
Javascript :: create a form and submit it dynamically jquery 
Javascript :: js replace multiple 
Javascript :: ngstyle background url angular 
Javascript :: import svg react 
Javascript :: urlencoded limit nodejs express 
Javascript :: loading image in react js 
Javascript :: js set datetime 
Javascript :: c# print object to json 
Javascript :: save canvas as image from website 
Javascript :: export all functions 
Javascript :: uploading file with fetch 
Javascript :: aos initial configuration vue 
Javascript :: jquery make visible 
Javascript :: discord client.send_message js 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =