Action creators are defined ahead of time and passed around as functions. The simplest action has only a type:
The format may feel familiar. Many redux tools use this pattern, namely redux-actions, which had a strong influence on retreon.
Now, plain actions have limited usefulness. Actions typically send more data into the reducer. We do this through
action.payload which can contain arbitrary data.
The action payload is whatever parameter you pass (
15), and the type of the payload is declared using a generic (
TypeScript will not let you pass a payload without defining the type.
Side effects belong with action creators. Associating an effect with an action means it automatically generates an audit log, simplifies bug reproduction, and helps keep view state synchronized with redux.
Wrap an effect with an action creator by passing it to
Whatever the effect returns becomes the action payload.
If your side effect returns a promise everything works exactly the same, except you would use
async has several advantages. You get optimistic reducer updates and rejection error handling out of the box.
What happens if the side effect goes wrong? Just throw an error. Retreon detects thrown exceptions and automatically dispatches an update to redux.
Say you're trying to access a key in
localStorage, but the client's browser rejects access as part of their security policy.
If the action creator throws an exception, retreon will take notice and dispatch a corresponding error action, giving reducers a chance to cleanly recover.
Note that retreon doesn't swallow errors - you'll still see the exception in your console.
If you've played around with retreon, you might've noticed that all the action creators return generator functions. That's why retreon requires middleware. It enables some powerful patterns, including the basis for error handling and the multiple dispatch of async actions.
It can also make testing slightly more cumbersome. It's recommended to test your actions and reducers against a real redux store, using the real middleware. Only your effects should be mocked.
Generators as action creators are a powerful and advanced pattern. Unless you're doing something very unusual, you probably won't need it.