Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transition to another route on successful async redux action

I have a pretty simple set of react components:

  • container that hooks into redux and handles the actions, store subscriptions, etc
  • list which displays a list of my items
  • new which is a form to add a new item to the list

I have some react-router routes as follows:

<Route name='products' path='products' handler={ProductsContainer}>   <Route name='productNew' path='new' handler={ProductNew} />   <DefaultRoute handler={ProductsList} /> </Route> 

so that either the list or the form are shown but not both.

What I'd like to do is to have the application re-route back to the list once a new item has been successfully added.

My solution so far is to have a .then() after the async dispatch:

dispatch(actions.addProduct(product)   .then(this.transitionTo('products')) ) 

Is this the correct way to do this or should I fire another action somehow to trigger the route change?

like image 361
Clarkie Avatar asked Sep 16 '15 15:09

Clarkie


People also ask

Can I redirect from Redux action?

We can redirect after a Redux action is committed. To do that, we install the history package. Then we can create a function like: import { createBrowserHistory } from 'history'; const browserHistory = createBrowserHistory(); const actionName = () => (dispatch) => { axios .

How do I redirect to another page in react?

To redirect to another page on button click in React: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function, passing it the path - navigate('/about') . The navigate() function lets us navigate programmatically.

Are Redux actions asynchronous?

By default, Redux's actions are dispatched synchronously, which is a problem for any non-trivial app that needs to communicate with an external API or perform side effects. Redux also allows for middleware that sits between an action being dispatched and the action reaching the reducers.

How do you redirect after form submit react?

To redirect on form submit using React Router: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function passing it the path - navigate('/contacts') . The navigate() function lets us navigate programmatically.


1 Answers

If you don't want to use a more complete solution like Redux Router, you can use Redux History Transitions which lets you write code like this:

export function login() {   return {     type: LOGGED_IN,     payload: {       userId: 123     }     meta: {       transition: (state, action) => ({         path: `/logged-in/${action.payload.userId}`,         query: {           some: 'queryParam'         },         state: {           some: 'state'         }       })     }   }; } 

This is similar to what you suggested but a tiny bit more sophisticated. It still uses the same history library under the hood so it's compatible with React Router.

like image 132
Dan Abramov Avatar answered Oct 06 '22 10:10

Dan Abramov