简体   繁体   English

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

[英]How push route to another path in react-redux connected component

i wan't to make conditional routing in my component. 我不会在组件中进行条件路由。 I'm using react-redux. 我正在使用react-redux。 I have tried different things like but getting undefined error. 我尝试了其他类似的尝试,但是却遇到未定义的错误。

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

so what i wan't to do, on component will mount i'm checking some boolean value from my redux state if this value will return false i want to redirect user to another route. 所以我不想做什么,在组件上将挂载我正在从我的redux状态检查一些布尔值,如果该值将返回false我想将用户重定向到另一条路由。

my component is: 我的组件是:

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)

Like in example i want to redirect in component will mount user to another path. 像示例中一样,我要在组件中重定向将用户挂载到另一个路径。 How i can do that? 我该怎么做?

You need Redirect 您需要重定向

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

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

Do you mean "history is undefined"? 您的意思是“历史未定义”吗? You can get history from parent component 您可以从父组件获取历史记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM