Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React Router:如何在路由回调中替换此调用_Reactjs_React Router - Fatal编程技术网

Reactjs React Router:如何在路由回调中替换此调用

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

当设置react路由器应用程序时,我经常在回调上设置它以表示授权过滤器。这让我想到了这样的设计:

# 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