Reactjs 如何通过react路由器v4重定向

Reactjs 如何通过react路由器v4重定向,reactjs,react-router,Reactjs,React Router,基于isLoggedIn我需要路由到不同的组件 我正在获取登录页面,但无法呈现主页 {this.state.isLoggedIn ? ( <Route path='/home' render={() => <HomePage onLogout={this.onLogout} />} /> ) : ( <Route exact path='/' componen

基于
isLoggedIn
我需要路由到不同的组件

我正在获取登录页面,但无法呈现主页

{this.state.isLoggedIn ?
    (
      <Route
        path='/home'
        render={() => <HomePage  onLogout={this.onLogout} />}
      />
    )
    :
    (
      <Route exact path='/' component={() => <LoginPage onLogin={this.onLogin} />}/>
    )
}
{this.state.isLoggedIn?
(
}
/>
)
:
(
}/>
)
}

您可以在参数中使用
this.props.history.push(myRoutes)
和send route。它会将您重定向到所需的路线

您可以在参数中使用
this.props.history.push(myRoutes)
和send route。它会将您重定向到所需的路线

您可以通过呈现将导航到新位置的
来重定向。新位置将覆盖历史堆栈中的当前位置,就像服务器端重定向(HTTP 3xx)一样

从'react router'导入{Route,Redirect}
(
洛格丁(
) : (
)
)}/>
通过重定向传递道具

<Redirect to={{
    pathname: '/dashboard',
    state: { id: '123' }
}} />


然后,您可以通过
此.props.location.state.id访问prop

您可以通过呈现
将导航到新位置来重定向。新位置将覆盖历史堆栈中的当前位置,就像服务器端重定向(HTTP 3xx)一样

从'react router'导入{Route,Redirect}
(
洛格丁(
) : (
)
)}/>
通过重定向传递道具

<Redirect to={{
    pathname: '/dashboard',
    state: { id: '123' }
}} />

然后,您可以通过
this.props.location.state.id访问prop

从父组件中,将
this.state.isLoggedIn作为道具传递给LoginPage和HomePage组件

在登录页面组件中

render(){
  if(this.props.isLoggedIn){
   return <Redirect to='/home' />
  }

  return ( your login page )
}
render(){
if(this.props.isLoggedIn){
返回
}
返回(您的登录页面)
}
在主页组件中使用重定向路径执行相同操作。

使用

从父组件中,将
this.state.isLoggedIn作为道具传递给LoginPage和HomePage组件

在登录页面组件中

render(){
  if(this.props.isLoggedIn){
   return <Redirect to='/home' />
  }

  return ( your login page )
}
render(){
if(this.props.isLoggedIn){
返回
}
返回(您的登录页面)
}

在主页组件中使用重定向路径
'/'
执行相同操作。

如何将道具发送到此重定向中的组件让我在回答中添加更多示例@ReNinja I添加了通过重定向传递道具的示例。请检查我在哪里可以指定组件名称主页我想你可以检查这个教程有返回();因此,您可以为路由声明组件,并重定向到重定向
第36行:解析错误:意外标记,应为“,”34 | 36 | onLogout:{this.onLogout}^37 |}/>38 |]:(39 |
如何将道具发送到此重定向中的组件让我添加更多示例作为回答。@ReNinja我添加了通过重定向传递道具的示例。请检查我可以在何处指定组件名称主页我想您可以检查此教程是否有返回();因此,您可以为路由声明组件,并在重定向
第36行:解析错误:意外标记,预期”,“34 | 36 | onLogout:{this.onLogout}^37 |}/>38 |)中重定向到routeGetting解析错误:(39 |
可以在渲染()中重定向吗函数?以及“”和“this.props.history.push('/home');”之间的区别是什么?很抱歉回答得太晚。是的,在render中重定向是可以的,因为它是react的组件。这两个函数之间的区别是:在render()函数中重定向可以吗?以及“”和“this.props.history.push”之间的区别是什么(“/home”);“很抱歉回复太晚。是的,可以在render中重定向,因为它是react的组件。这两者之间的区别如下: