connected-react-router: Redirection not work after action
Hi, I have using connected-react-router 6.1.0 in my react project stack.
react -> 16.7.0 react-router-dom -> 4.3.1 react-redux -> 6.0.0 redux-thunk -> 2.3.0
I have configurated my app, and router works perfectly (I can use the Redirect component from react-router). The problem that I have detected is produced when I am doing an API call (action) and before I want to redirect (promise finish). I don´t know why the location path change to the right URL but the page component not is loaded, (If I reload the page I can continue).
Example:
import React, { Component } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { push } from "connected-react-router";
import {redirectAction} from "./actions.js";
sampleComponent extends Component {
constructor (){...}
formSending = async () => {
await this.props.firstApiCall() // like 5 seconds
await this.props.secondApiCall() // like 500ms
// Here I want to redirect I have try with this:
this.props.redirect("/new-route");
this.props.push("/new-route");
// I have try creating an action how explain in the FAQ
this.props.redirectWithAction("new-route");
}
}
mapDispatchToProps = (dispatch) => ({
redirect: bindActionCreators(push, dispatch),
push,
redirectWithAction: bindActionCreator(redirectAction, dispatch)
});
export default connect(null, mapDispatchToProps)(sampleComponent);
/// actions.js
export const redirectAction = route => dispatch => {
console.log("Redirecting to", route);
dispatch(push(route));
};
Anyone can help me, please?
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 15
- Comments: 23 (4 by maintainers)
Commits related to this issue
- Address part of the confusions highlighted in #230 — committed to AJamesPhillips/connected-react-router by AJamesPhillips 5 years ago
- Address part of the confusions highlighted in #230 (#277) — committed to supasate/connected-react-router by AJamesPhillips 5 years ago
I had a same problem. This was indeed very simple problem. I looked at the installation steps and found that I was using BrowserRouter under ConnectedRouter
Router.js
index.js
Same here.
I also installed unnecessary peer dependencies just to be sure
Similar problem under v6.2.1. Locking connected-react-router to v6.0.0 helps me.
I have same issue, the installations step seems to be ok (connectedRouter is present and hasn’t other router below, no pureComponenet are used, component are wrapped in withRouter), i see the action correctly dispatched but the state router is not updated (and of course also url and page content are not updated)
same for push and goBack
package.json
@foodforarabbit that’s problem code. I was doing the same thing nesting
BrowserRouterunderConnectedRouterand it caused theRedirectto update the page url but the redux state was not being updated. Removing the nestedBrowserRouterallowed it to then work fine. Eg:The application state was incorrectly not updating:
But after removing the nested
BrowserRouterthen was correctly being set to:This is still happening in v6.8.0.
Downgrading to v6.0.0 was the solution, but like everyone else, I’d like to know why