Answer the question
In order to leave comments, you need to log in
How to do a redirect?
Good evening, I'm interested in how to properly implement a redirect in a React / Redux and React Router bundle
. I have a container for a component, the container is wrapped in WithRouter and Connect:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import RegAuth from '../components/RegAuth';
import {
regNewUser,
authUser
} from '../redux/regAuth/actions';
class RegAuthContainer extends Component {
render() {
if (this.props.isAuth) {
this.props.history.push('/personal');
}
return <RegAuth regAuthError={this.props.regAuthError} isAuth={this.props.isAuth} authUser={this.props.authUser} regNewUser={this.props.regNewUser} fetching={this.props.fetching} token={this.props.token} />
}
}
const mapStateToProps = state => {
return {
fetching: state.regAuth.fetching,
isAuth: state.regAuth.isAuth,
token: state.regAuth.token,
regAuthError: state.regAuth.regAuthError
}
}
const mapDispatchToProps = {
authUser,
regNewUser
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(RegAuthContainer));
render() {
if (this.props.isAuth) {
this.props.history.push('/personal');
}
render() {
console.log('Rendered');
const { isAuthFormOpened } = this.state;
const switcherButtonText = isAuthFormOpened ? 'Register now' : 'Already have acc?';
const { regAuthError, fetching, token } = this.props; // Добавить модный лоадер, если не будет лень
return (
<>
<Header />
{regAuthError && <Error errorText={regAuthError} />}
{isAuthFormOpened ? <AuthForm authUserFunc={this.authUser} /> : <RegForm regNewUserFunc={this.regNewUser} />}
<FormSwitch switcherButtonText={switcherButtonText} FormSwitcher={this.FormSwitcher}/>
<Footer />
</>
)
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question