question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: side-effects and dispatch

See original GitHub issue

in 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:open
  • Created 6 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

2reactions
durocommented, Oct 6, 2017

@icopp and @tony-kerz:

Based on the following in this projects README, I have a feeling the author considers this an anti-pattern:

Async actions in redux are often done using redux-thunk or other middlewares. The problem with this approach is that it makes it too easy to use dispatch sequentially, and dispatch multiple “actions” as the result of the same interaction/event, where they probably should have just been a single action dispatch.

This can be problematic because we are treating several dispatches as all part of a single transaction, but in reality, each dispatch causes a separate rerender of the entire component tree, where we not only pay a huge performance penalty, but also risk the redux store being in an inconsistent state.

redux-pack helps prevent us from making these mistakes, as it doesn’t give us the power of a dispatch function, but allows us to do all of the things we were doing before.

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.

1reaction
YaaMecommented, Mar 31, 2018

Why not move it to middleware? like that

export const analyticsMiddleware = store => next => action => {  
    switch (action.type) {
        case DO_FOO: 
            const { meta } = action;
            const lifecycle = meta ? meta[KEY.LIFECYCLE] : null;

            switch (lifecycle) {
                case LIFECYCLE.SUCCESS: store.dispatch(SOMETHING_SUCCEEDED); break;
                case LIFECYCLE.FAILURE: store.dispatch(SOMETHING_FAILED); break;
            }
        default: ;
    }
    return next(action);
}

😛

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - How to dispatch variable side effects in Redux - Stack ...
In my TypeScript project, I present the user with a branching dialogue. Text is displayed and options are presented for the user, which...
Read more >
3 common approaches to side-effects in Redux apps
What is a side-effect? ... The natural Redux flow is this: some action is dispatched, and as a consequence, some state is changed....
Read more >
Chapter 6. Handling complex side effects - Redux in Action
In chapter 4, you handled simple side effects within action creators by leveraging ... #3 Based on the results of a side effect,...
Read more >
Redux side effects and me - Medium
Where thunks fall apart, sagas prevail: testing the sagas is nothing more than seeing if they ask the right questions (or yield the...
Read more >
Home - Side Effects - USA TODAY NETWORK News
many questions remain. Cathy Candisky and Darrel Rowland | The Columbus Dispatch. Sept. 30, 2019. Medicaid Director Maureen Corcoran said Ohio is getting ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found