2

I am trying to use history.push method in my redux react app. Its working fine but the problem is my component won't change, do you guys know why?

route.js:

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import { history } from '../helper/history'

export default class route extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <Router history={history}>
          <Switch>
            <Route exact path="/login" component={Login} />
            <Route path="/error" component={Error} />
          </Switch>
        </Router>
      </Provider>
    )
  }
}

Redux action.js where a history is called, this is where I dispatch my actions:

export const loginRequest = () => {
  return {
    type: userType.LOGIN_REQUEST,
  }
}

export const loginSuccess = () => {
  return {
    type: userType.LOGIN_SUCCESS,
  }
}

export const loginFailure = () => {
  return {
    type: userType.LOGIN_FAILURE,
  }
}

export const fetchUser = (data) => {
  return (dispatch) => {
    dispatch(loginRequest)
    axios
      .post(env.API_URL + 'login', { email: data.email, password: data.password })
      .then((res) => {
        dispatch(loginSuccess(res.data.user))
        history.push({
          pathname: '/profile',
        })
      })
      .catch((err) => {
        dispatch(loginFailure(error))
      })
  }
}
3
  • 1
    I have added some code Commented May 24, 2020 at 18:45
  • even if I add some profile component and route it showing the same issue ` <Route path="/profile" component={Profile} />. history.push('/profile')` is working but my component wont change unless I refresh's my page Commented May 25, 2020 at 5:41
  • import {BrowserRouter as Router,Route, Switch} from "react-router-dom"; Commented May 25, 2020 at 5:45

1 Answer 1

1

As you are providing history props, you should use Router. See this, Router and BrowserRouter:

import { Router, Route, Switch } from 'react-router-dom'

instead of

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.