当前位置: 代码迷 >> JavaScript >> 如何将路由推送到react-redux连接的组件中的另一条路径
  详细解决方案

如何将路由推送到react-redux连接的组件中的另一条路径

热度:40   发布时间:2023-06-07 15:53:26.0

我不会在组件中进行条件路由。 我正在使用react-redux。 我尝试了其他类似的尝试,但是却遇到未定义的错误。

this.props.history.push('/pathname')

所以我不想做什么,在组件上将挂载我正在从我的redux状态检查一些布尔值,如果该值将返回false我想将用户重定向到另一条路由。

我的组件是:

import React, {Component} from 'react'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'

class SomeComponent extends Component {
componentWillMount() {
        this.props.someFunc()
        if ( this.props.userLogin[0].isUserLogin === false ) {
            /// here i want to redirect to another route 
        } 
    }
render() {
   return(
     <div>Some Content</div>
   )
  }
}

const mapStateToProps = state => ({
    userLogin: state.UserLogin,
  })

  function mapDispatchToProps(dispatch) {
    return {
      someFunc: bindActionCreators(someAction, dispatch)
    }
  } 

  export default connect(mapStateToProps, mapDispatchToProps)(SomeComponent)

像示例中一样,我要在组件中重定向将用户挂载到另一个路径。 我该怎么做?

您需要重定向

import {Redirect} from 'react-router-dom';

{boolFlag && <Redirect  to="/hello" />}  // put this in render

您的意思是“历史未定义”吗? 您可以从父组件获取历史记录

  相关解决方案