简体   繁体   English

为什么在 ReactJs 中的私有路由中传递道具无法正常工作

[英]why Passing Props in Private Route in ReactJs not working properly

Here is mycode of the file PrivateRoute.js这是文件 PrivateRoute.js 的 mycode

import React from "react";
import {Route,Redirect} from "react-router-dom"
import {isAuthenticated} from "./index"


const PrivateRoutes = (props)=>{

 return(
    isAuthenticated()?(<Route
    render={ (props) => 
      <component {...props}/>} />):
   (<Redirect to={{ pathname:"/signin"}}/>)
  //   <h1>hey there</h1>
   )
   
 }


 export default PrivateRoutes;

In this code it is saying that value of props is read but never used but iam using it in render function,destructuring is also not working here.在这段代码中,它说 props 的值已读取但从未使用过,但我在渲染函数中使用它,解构在这里也不起作用。 Here isAuthenticated() is my boolean function .这里 isAuthenticated() 是我的布尔函数。 If it evaluates to true i want to get on more route to user dashboard.如果它评估为真,我想获得更多通往用户仪表板的路线。

This is how my routes.js file looks like这就是我的 routes.js 文件的样子

import React from 'react'
import {BrowserRouter,Route,Switch} from "react-router-dom";
import AdminRoutes from './auth/helper/AdminRoutes';
import PrivateRoutes from './auth/helper/PrivateRoutes';
import Home from './core/Home';
import AdminDashboard from './user/AdminDashBoard';
import Signin from './user/Signin';
import Signup from './user/Signup';
import UserDashboard from './user/UserDashBoard';


 function Routes() {
 return (
   <BrowserRouter>
  
   <Switch>
     <Route exact path='/' component={Home}  />
     <Route exact path="/signup" component={Signup} />
     <Route exact path="/signin" component={Signin} />
     <PrivateRoutes component="UserDashboard" exact path="/user/dashboard"/>
     <AdminRoutes exact path="/admin/dashboard" component={AdminDashboard}/>
   </Switch>

   </BrowserRouter>
  )
 }

 export default Routes

Help me to solve this problem.帮我解决这个问题。

This is because you have declared * two functions, each taking a props object argument, but only the inner/nested function's props is referenced.这是因为您已经声明了 *两个函数,每个函数都有一个props对象参数,但只引用了内部/嵌套函数的props

Rename the nested props to something else like routeProps and spread both to the rendered component.将嵌套的 props 重命名为routeProps之类的东西,并将它们传播到渲染的组件。 Remember also that valid React component names are PascalCased.还要记住,有效的 React 组件名称是 PascalCased。

Example:例子:

const PrivateRoutes = ({ component: Component, ...props }) => {
  return isAuthenticated()
    ? (
      <Route
        render={(routeProps) => (
          <Component {...props} {...routeProps} />
        )}
      />
    ) : <Redirect to={{ pathname:"/signin"}}/>;
}

Then also fix the PrivateRoutes component use to pass a value React component reference instead of a string.然后还修复PrivateRoutes组件用于传递值 React 组件引用而不是字符串。

<Switch>
  <Route path="/signup" component={Signup} />
  <Route path="/signin" component={Signin} />
  <PrivateRoutes component={UserDashboard} path="/user/dashboard" />
  <AdminRoutes path="/admin/dashboard" component={AdminDashboard} />
  <Route path='/' component={Home}  />
</Switch>

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

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