Source:  Twitter logo

I'm working on react-router-dom app for the first time since I started using react/redux and I'm having a trouble with using Redirect component.

I want to fire Redirect when authentication is successful but it does not redirect to the page that I want after authentication.

index.js

import { BrowserRouter as Router, Route } from 'react-router-dom';
import { createStore, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';

import App from './components/app';
import Page1 from './containers/page1';
import Signin from './containers/auth/signin';

import Reducers from './reducers';

const createStoreWithMiddleware = applyMiddleware(ReduxThunk)(createStore);
const store = createStoreWithMiddleware(Reducers);

ReactDOM.render(
  <Provider store={store}>
    <Router>
      <div>
        <Route path="/" component={App}/>
        <Route path="/" component={Signin}/>
        <Route path="/signin" component={Signin}/>
        <Route path="/page1" component={Page1}/>
      </div>
    </Router>
  </Provider>
, document.querySelector('.container')); 

signin.js

import { BrowserRouter as Route, Redirect } from 'react-router-dom';

import { userSignin } from '../../actions/index';

class Signin extends Component {
  constructor(props) {
    super(props);

    this.state = { username: '', password: '' };

    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleUsernameChange = this.handleUsernameChange.bind(this);
    this.handlePasswordChange = this.handlePasswordChange.bind(this);
  }

  componentWillUpdate(nextProps) {
    if (this.props.authenticated !== nextProps.authenticated) {
      if (nextProps.authenticated) {
        console.log('true?', nextProps.authenticated)
        return (
          <Redirect to="/page1" />
        );
      }
    }
  }

  handleSubmit(e) {
    e.preventDefault();
    this.props.userSignin({ username: this.state.username, password: this.state.password });
    this.setState({ username: '', password: '' });
  }
  .....

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <input
          placeholder="username"
          value={this.state.username}
          onChange={this.handleUsernameChange}
        />
        <input
          placeholder="password"
          type="password"
          value={this.state.password}
          onChange={this.handlePasswordChange}
        />
        <span>
          <button type="submit">SIGNIN</button>
        </span>
        {this.renderError()}
      </form>
    );
  }
}

function mapStateToProps(state) {
  return {
    authenticated: state.auth.authenticated,
    errMsg: state.auth.err
  };
}

function mapDispatchToProps(dispatch) {
  return {
    userSignin: ({ username, password }) => dispatch(userSignin({ username, password }))
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(Signin);

In this code, this does log true? but it does not redirect to the /page1.

As I mentioned, this is my first time using react-router-dom library and I apologize for any dumb mistake that I have in my code.

Thank you and please insight me!

Instead of Redirect use this.props.history.push('/path1') and wrap the connect() in withRouter(import from react-router) like this:

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Signin));

If you want to use Redirect, maintain the authenticated value in the local state , update it in componentWillReceiveProps and conditionally render the <Redirect /> in render method.

5 users liked answer #0dislike answer #05
Ravi Theja profile pic
Ravi Theja

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).