组件不刷新的反应路由器路由问题

react-router routing problem with component not refreshing

我正在尝试在我的 redux React 应用程序中使用 history.push 方法。它工作正常,但问题是我的组件不会改变,你们知道为什么吗?

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 调用历史记录的地方,这是我发送操作的地方:

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))
      })
  }
}

当你提供 history 道具时,你应该使用 Router。见 this, Router and BrowserRouter:

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

而不是

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