In the current version of React Router (v3) I can accept a server response and use browserHistory.push
to go to the appropriate response page. However, this isn't available in v4, and I'm not sure what the appropriate way to handle this is.
In this example, using Redux, components/app-product-form.js calls this.props.addProduct(props)
when a user submits the form. When the server returns a success, the user is taken to the Cart page.
// actions/index.js
export function addProduct(props) {
return dispatch =>
axios.post(`${ROOT_URL}/cart`, props, config)
.then(response => {
dispatch({ type: types.AUTH_USER });
localStorage.setItem('token', response.data.token);
browserHistory.push('/cart'); // no longer in React Router V4
});
}
How can I make a redirect to the Cart page from function for React Router v4?
React router V4 now allows the history prop to be used as below:
The value then can be accessed wherever the location prop is available as
state:{value}
not component state.I offer one more solution in case it is worthful for someone else.
I have a
history.js
file where I have the following:Next, on my Root where I define my router I use the following:
Finally, on my
actions.js
I import History and make use of pushLaterThis way, I can push to new actions after API calls.
Hope it helps!
Nasty question, took me quite a lot of time, but eventually, I solved it this way:
Wrap your container with
withRouter
and pass history to your action inmapDispatchToProps
function. In action use history.push('/url') to navigate.Action:
Container:
This is valid for React Router v4.x.
step one wrap your app in Router
Now my entire App will have access to BrowserRouter. Step two I import Route and then pass down those props. Probably in one of your main files.
Now if I run console.log(this.props) in my component js file that I should get something that looks like this
Step 2 I can access the history object to change my location
Also I'm just a coding bootcamp student, so I'm no expert, but I know you can also you use
Correct me if I'm wrong, but when I tested that out it reloaded the entire page which I thought defeated the entire point of using React.
I was able to accomplish this by using
bind()
. I wanted to click a button inindex.jsx
, post some data to the server, evaluate the response, and redirect tosuccess.jsx
. Here's how I worked that out...index.jsx
:request.js
:success.jsx
:So by binding
this
topostData
inindex.jsx
, I was able to accessthis.props.history
inrequest.js
... then I can reuse this function in different components, just have to make sure I remember to includethis.postData = postData.bind(this)
in theconstructor()
.According to React Router v4 documentation - Redux Deep Integration session
Deep integration is needed to:
However, they recommend this approach as an alternative to the "deep integration":
So you can wrap your component with the withRouter high order component:
export default withRouter(connect(null, { actionCreatorName })(ReactComponent));
which will pass the history API to props. So you can call the action creator passing the history as a param. For example, inside your ReactComponent:
Then, inside your actions/index.js: