-1

我有一个私有路由组件,如果我有用户,它将打开到布局页面,如果没有,它将重定向到登录页面路由,即登录页面。问题是我的应用页面上有一个由布局页面调用的函数,所以我将它作为道具传递给布局页面,如下所示

  <PrivateRoute>
     <Layout send_id={send_id}/>
   </PrivateRoute> 

但是当我这样做时,它会忽略私有路由中的重定向

export default function PrivateRoute({component :Component, ...rest}) {
const {currentUser} = useAuth()

console.log({currentUser})
return (
    <Route
        {...rest}
        render={props=>{
           return currentUser ? <Component {...props}/> :<Redirect to="/landingPage" />
        }}
        >
        
    </Route>
)

}

但是当我将应用程序页面上的路线更改回添加功能之前的路线时,它可以完美运行

 <PrivateRoute exact path="/" component = {Layout}/>
4

1 回答 1

0

将组件更改为子字段,这是子道具,而不是组件道具。

export default function PrivateRoute({children :Component, ...rest}) {
const {currentUser} = useAuth()

console.log({currentUser})
return (
    <Route
        {...rest}
        render={props=>{
           return currentUser ? <Component {...props}/> :<Redirect to="/landingPage" />
        }}
        >
        
    </Route>
)
于 2021-01-17T01:40:17.477 回答