【发布时间】:2021-01-21 17:50:34
【问题描述】:
所以我在 React 中使用 PrivateRoute 来提供一些组件。在我指定如下的确切路径之前,一切都运行良好:
<PrivateRoute exact path="/info/:id" render={(props) => <BookInfo {...props} />} />
如果我使用Route 正常提供上述路径,则上述路径可以正常工作,但通过PrivateRoute 进行路由会给我以下错误。
PrivateRoute.js
import React from 'react'
import { Route, Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
const PrivateRoute = ({ component: Component, auth, ...rest}) => (
<Route {...rest} render={ props =>
auth.isAuthenticated === true ?
(<Component {...props}/>) : (<Redirect to ='/login'/>)}/>
)
const mapStateToProps = state => ({
auth: state.auth
})
export default connect (mapStateToProps)(PrivateRoute);
我不知道是什么原因造成的,非常感谢一些帮助。
【问题讨论】:
-
PrivateRoute需要一个component属性才能被传递。您在调用<PrivateRoute exact path="/info/:id" render={(props) => <BookInfo {...props} />} />时缺少该道具 -
我该如何解决这个问题?
-
试试这个
<PrivateRoute exact path="/info/:id" component={BookInfo} />
标签: javascript reactjs react-router react-router-dom