question: side-effects and dispatch
See original GitHub issuein this section of the doc on side-effects, it shows something like:
import { sendAnalytics } from '../analytics';
import { doFoo } from '../api/foo';
export function userDoesFoo() {
return {
type: DO_FOO,
promise: doFoo(),
meta: {
onSuccess: (result, getState) => {
const userId = getState().currentUser.id;
const fooId = result.id;
sendAnalytics('USER_DID_FOO', {
userId,
fooId,
});
}
}
}
}
in the above sample, sendAnalytics
looks a lot like an action creator (with type and payload as args), but submitting actions to redux requires a reference to dispatch
somewhere.
is the implication that sendAnalytics
submits a redux action, and if so, what is the implied strategy for obtaining a reference to dispatch
?
or maybe, i’m asking the wrong question, but my general question is:
what would be the idiomatic way to manage a side-effect with redux-pack that wants to submit another redux action?
or is that an anti-pattern that redux-pack is specifically attempting to avoid?
apologies for being dense, i admittedly struggle with extracting “best practices” for side-effects from related discussions…
my immediate use-case is: on the failure of a data-fetch action i want to submit a subsequent ui-centric action concerned with managing errors (e.g. notify the user visually of an error)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:9
Top GitHub Comments
@icopp and @tony-kerz:
Based on the following in this projects README, I have a feeling the author considers this an anti-pattern:
Given this encouragement, I was able to take a step back and look at our case where we thought a dispatch side-effect was needed, and see how it could be combined into a single action.
Why not move it to middleware? like that
😛