我正在構建一個登錄系統 - 當用戶填寫表單時 - 我創建了一個用於「驗證」用戶的redux動作 - 所以當this.props .authData.isLogged爲真 - 我想要調用導航功能以將用戶彈回到主頁。但是,this.props.router是不確定的?但我已經定義了與路由器?Reactjs/Redux - this.props.router undefined
import React, { Component } from 'react'
//import { render } from 'react-dom'
//import { withRouter } from 'react-router-dom'
import { withRouter } from 'react-router';
// components
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { fetchAuthentication } from '../../actions/authAction';
import LoginSyncValidationForm from './LoginSyncValidationForm'
// this is a class because it needs state
class Login extends Component {
constructor(props, context) {
super(props, context);
this.submit = this.submit.bind(this);
}
componentDidMount() {
// console.log('this', this)
}
submit (data) {
this.props.fetchAuthentication(data);
}
navigate() {
console.log("navigate", this)
//this.props.router.push('/home');
}
render() {
console.log(this.props.authData)
if(this.props.authData.isLogged){
this.navigate();
}
return (
<div className="Page">
<h2>Login</h2>
<LoginSyncValidationForm onSubmit={this.submit} />
</div>
)
}
}
function mapStateToProps(state) {
return {
authData: state.auth
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ fetchAuthentication }, dispatch);
}
const {object} = React.PropTypes
Login.propTypes = {
router: object
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Login))
https://stackoverflow.com/questions/39174814/using-react-router-withrouter –
我試圖定義道具類型更早 - 它仍然沒有露面。 –
可能重複[編程導航使用反應路由器](https://stackoverflow.com/questions/44127739/programatically-navigate-using-react-router) –