I have a pretty simple set of react components:
container
that hooks into redux and handles the actions, store subscriptions, etclist
which displays a list of my itemsnew
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?
For those that are using a middleware API layer to abstract their usage of something like isomorphic-fetch, and also happen to be using redux-thunk, you can simply chain off your
dispatch
Promise inside of your actions, like so:This reduces the need for adding libraries into your code as suggested here, and also nicely co-locates your actions with their redirects as done in redux-history-transitions.
Here is what my store looks like:
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:
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.
I know I am little late in the party as react-navigation is already included in the react-native documentation, but still it can be useful for the user who have used/using Navigator api in their apps. what I tried is little hackish, I am saving navigator instance in object as soon as renderScene happens-
my api file is something lke this
now in your actions you can simply call
you just need to import your api from the module.
If you're using react-redux and react-router, then I think this link provides a great solution.
Here's the steps I used:
history
prop to your component, either by rendering your component inside a react-router<Route/>
component or by creating a Higher Order Component usingwithRouter
.to
).history
andto
.history.push(to)
.Not the answer you're looking for? Browse other questions tagged javascript reactjs react-router redux or ask your own question.
asked
viewed
26,976 times
active
3 months ago
Linked
Related
Hot Network Questions