Reactjs React Router:如何在路由回调中替换此调用
当设置react路由器应用程序时,我经常在回调上设置它以表示授权过滤器。这让我想到了这样的设计:Reactjs React Router:如何在路由回调中替换此调用,reactjs,react-router,Reactjs,React Router,当设置react路由器应用程序时,我经常在回调上设置它以表示授权过滤器。这让我想到了这样的设计: # before, in my root component requireAuth() { ... } noAuth() { ... } render() { return ( <Router history={this.props.history}> <Route path='/' component={App} onEnter={this.requir
# before, in my root component
requireAuth() { ... }
noAuth() { ... }
render() {
return (
<Router history={this.props.history}>
<Route path='/' component={App} onEnter={this.requireAuth}>
<Route path='toys' component={Toys}/>
...
<Route path='/auth' component={Auth} onEnter={this.noAuth}>
...
</Router>
)
}
所以,这似乎并没有减少它,我得到了一些错误。首先,我将所有路由封装在主Route
组件中。从反应路由器的角度来看,这是正确的吗?我可以有一种空的封装组件吗
第二个问题是:现在如何在服务器端绑定这些this.
回调?由于我遵循教程,我的express router看起来像这样:
import routes from './src/components/routes';
...
app.get('*', (req, res) => {
match(
{ routes, location: req.url },
(err, redirectLocation, renderProps) => {
....
但它与:
onEnter: undefined.requireAuth,
^
TypeError: Cannot read property 'requireAuth' of undefined
这是正确的,因为routes常量不绑定到任何上下文
或者在react路由器中有没有更合适的方法来实现这一点,而将回调绑定到路由是错误的方法
onEnter: undefined.requireAuth,
^
TypeError: Cannot read property 'requireAuth' of undefined