Ich baue ein Login-System - wenn der Benutzer das Formular ausfüllt - Ich habe eine Redux-Aktion erstellt, um den Benutzer zu "authentifizieren" - also wenn this.props .authData.isLogged ist wahr - Ich möchte die Navigationsfunktion aufrufen, um den Benutzer auf die Homepage zu verweisen. This.props.router ist jedoch nicht definiert? Aber ich habe den withRouter definiert?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 –
Ich habe versucht, die prop Typen früher definieren - es immer noch nicht nach oben zeigte. –
Mögliches Duplikat von [Programmatisch navigieren mit reaktiven Router] (https://stackoverflow.com/questions/44127739/programatically-navigate- using-reactor-router) –