我正在尝试在我的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))
})
}
}
答案 0 :(得分:1)
在提供history
道具时,应使用Router
。参见https://codepen.io/farisk/pen/zYvXvox,this和Router:
import { Router, Route, Switch } from 'react-router-dom'
代替
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'