Reactjs 如何限制对路线的访问

Reactjs 如何限制对路线的访问,reactjs,forms,redirect,react-router,submit,Reactjs,Forms,Redirect,React Router,Submit,我的App.js中有 render() { return ( <Router history={customHistory}> <div> <Header/> <Switch> <Route exact path="/" component={HomePage}/>

我的App.js中有

render() {
    return (
        <Router history={customHistory}>
            <div>
                    <Header/>
                    <Switch>
                    <Route exact path="/" component={HomePage}/>
                    <Route exact path="/test" component={ThankYou} />
                    <Route path="*" component={NotFound} status={404}/>
                    </Switch>
                    <Footer/>                        
            </div>
        </Router>
    );
}
render(){
返回(
);
}
这是溃败,我有联系方式

如何确保管线上的组件


只能在成功后提交表单,如果您尝试在其他时间提交表单,则重定向到404

一种通用方法是创建
PrivateRoute

根据状态-
formSubmitted
,我们可以重定向到
notFound
或404路由

class ProtectedRoute extends Component {
  render() {
    const { component: Component, ...props } = this.props

    return (
      <Route 
        {...props} 
        render={props => (
          this.state.formSubmitted ?
            <Component {...props} /> :
            <Redirect to='/notFound' />
        )}
      />
    )
  }
}

class AllRoutes extends Component {
  render() {
    return (
      <Switch>
        <Route path='/login' component={Login} />
        <ProtectedRoute path='/test' component={ThankYou} />
        <Route path="*" component={NotFound} />
      </Switch>
    )
  }
}
类ProtectedRoute扩展组件{
render(){
const{component:component,…props}=this.props
返回(
(
这个.state.forms提交了吗?
:
)}
/>
)
}
}
类AllRoutes扩展组件{
render(){
返回(
)
}
}

一种通用方法是创建
privaterout

根据状态-
formSubmitted
,我们可以重定向到
notFound
或404路由

class ProtectedRoute extends Component {
  render() {
    const { component: Component, ...props } = this.props

    return (
      <Route 
        {...props} 
        render={props => (
          this.state.formSubmitted ?
            <Component {...props} /> :
            <Redirect to='/notFound' />
        )}
      />
    )
  }
}

class AllRoutes extends Component {
  render() {
    return (
      <Switch>
        <Route path='/login' component={Login} />
        <ProtectedRoute path='/test' component={ThankYou} />
        <Route path="*" component={NotFound} />
      </Switch>
    )
  }
}
类ProtectedRoute扩展组件{
render(){
const{component:component,…props}=this.props
返回(
(
这个.state.forms提交了吗?
:
)}
/>
)
}
}
类AllRoutes扩展组件{
render(){
返回(
)
}
}

我理解正确,我用app.js在文件夹中创建了ProtectedRoute类(在它里面我的路由),在它里面我写了你写的东西并在app.js里导入,然后我在它里面写了一个新的ProtectedRoute?我只是这样得到一个错误
我理解正确的上面的错误发生在,我用app.js在文件夹中创建了ProtectedRoute类(在它里面是我的路由),在它里面我写了你写的东西并在app.js里导入,然后我在它里面写了一个新的ProtectedRoute?我只是得到了一个这样的错误
上面的错误发生在