简体   繁体   English

在 react.js 中应用身份验证时不在屏幕上显示任何内容

[英]Not showing any thing on screen while applying authentication in react.js

I was applying authenication in my project in React.js by using protectes routes.我在 React.js 的项目中通过使用 protectes 路由来应用身份验证。 First i was using Redirect component from react-router-dom but then i have found out the changes they made in react-router-dom than i applied the navigate component.首先,我使用的是 react-router-dom 中的 Redirect 组件,但后来我发现他们在 react-router-dom 中所做的更改比我应用导航组件要多。

import {BrowserRouter,Routes,Route,Navigate} from 'react-router-dom';
import './App.css';
import Navigation from './components/shared/Navigation/Navigation';
import Authenticate from './pages/Authenticate/Authenticate';
import Home from './pages/Home/Home';
import Login from './pages/Login/Login';
import Register from './pages/Register/Register';

const isAuth = true;

function App() {
  return (
    <div className="App">

    <BrowserRouter>

    <Navigation/>

    <Routes>
      <Route  exact path='/' element={<Home/>}></Route>
      {/* <Route exact path='/register' element={<Register/>}></Route>
      <Route exact path='/login' element={<Login/>}></Route> */}
      {/* <Route exact path='/authenticate' element={<Authenticate/>}></Route> */}
      <GuestRoute  exact path='/authenticate' element={<Authenticate/>}></GuestRoute>
    </Routes> 

    </BrowserRouter>
     
    </div>
  );
}

const GuestRoute = ({children,...rest}) =>{
  return(
    <Route {...rest} render = {({location})=>{
      return isAuth ?(
         <Navigate  to = '/rooms'state = {{from : location}} replace />
       ) 
       :(
         children 
       )
   
     }}></Route>
  )
  
}

export default App;

This is my code after using the navigate component my screen not showing any thing there must be some kind of logical error in it.这是我使用导航组件后的代码,我的屏幕没有显示任何东西,其中一定存在某种逻辑错误。 Kindly help me to resolve this error.请帮我解决这个错误。

your app is showing nothing because the route /rooms does not match any routes in the <Routes /> component您的应用程序没有显示任何内容,因为路由/rooms<Routes />组件中的任何路由都不匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM