【发布时间】:2017-09-27 11:31:21
【问题描述】:
我有以下 react 功能组件来帮助支持使用 react-router 进行身份验证所需的路由。
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
isAuthenticated() ? (
<Component {...props}/>
) : (
<Redirect to={{
pathname: '/login',
state: {from: props.location }
}}/>
)
)}/>
)
我需要将它从一个函数组件转换为一个类组件,这样我才能利用 React.Component 的 componentDidMount 方法。不幸的是,我无法弄清楚如何迁移它。如果我按原样处理,我需要复制 Component 和 ...rest 参数,但我不知道该怎么做。我想我可以通过 this.props.component 获取 Component 参数,但我不确定如何拉...rest。我是 JSX 和 ES6 的新手,因此非常感谢任何帮助或指导。
【问题讨论】:
-
你能用这个功能组件展示代码吗?
标签: javascript reactjs react-router jsx