问题描述
我不会在组件中进行条件路由。 我正在使用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)
像示例中一样,我要在组件中重定向将用户挂载到另一个路径。 我该怎么做?
1楼
您需要重定向
import {Redirect} from 'react-router-dom';
{boolFlag && <Redirect to="/hello" />} // put this in render
2楼
您的意思是“历史未定义”吗? 您可以从父组件获取历史记录