I have an action that updates notification state of my application. Usually, this notification will be an error or info of some sort. I need to then dispatch another action after 5 seconds that will return the notification state to initial one, so no notification. The main reason behind this is to provide functionality where notifications disappear automatically after 5 seconds.
I had no luck with using setTimeout
and returning another action and can't find how this is done online. So any advice is welcome.
If you want timeout handling on selective actions, you can try the middleware approach. I faced a similar problem for handling promise based actions selectively and this solution was more flexible.
Lets say you your action creator looks like this:
timeout can hold multiple values in the above action
Your middleware implementation would look like this:
You can now route all your actions through this middleware layer using redux.
You can find some similar examples here
I would recommend also taking a look at the SAM pattern.
The SAM pattern advocates for including a "next-action-predicate" where (automatic) actions such as "notifications disappear automatically after 5 seconds" are triggered once the model has been updated (SAM model ~ reducer state + store).
The pattern advocates for sequencing actions and model mutations one at a time, because the "control state" of the model "controls" which actions are enabled and/or automatically executed by the next-action predicate. You simply cannot predict (in general) what state the system will be prior to processing an action and hence whether your next expected action will be allowed/possible.
So for instance the code,
would not be allowed with SAM, because the fact that a hideNotification action can be dispatched is dependent on the model successfully accepting the value "showNotication: true". There could be other parts of the model that prevents it from accepting it and therefore, there would be no reason to trigger the hideNotification action.
I would highly recommend that implement a proper next-action predicate after the store updates and the new control state of the model can be known. That's the safest way to implement the behavior you are looking for.
You can join us on Gitter if you'd like. There is also a SAM getting started guide available here.
its simple. use trim-redux package and write like this in componentDidMout or other place and kill it in componentWillUnmount.
The appropriate way to do this is using Redux Thunk which is a popular middleware for Redux, as per Redux Thunk documentation:
So basically it returns a function, and you can delay your dispatch or put it in a condition state.
So something like this is going to do the job for you:
Whenever you do setTimeout please make sure you also clear the timeout using clearTimeout when your component un mounts in componentWillUnMount life cycle method
Redux itself is a pretty verbose library, and for such stuff you would have to use something like Redux-thunk, which will give a
dispatch
function, so you will be able to dispatch closing of the notification after several seconds.I have created a library to address issues like verbosity and composability, and your example will look like the following:
So we compose sync actions for showing notifications inside async action, which can request some info the background, or check later whether the notification was closed manually.