Giter Club home page Giter Club logo

Comments (5)

tol-is avatar tol-is commented on May 28, 2024 1

Indeed, I experienced the same issue, and also ended up using a conditional statement, and also dispatched the action in a setTimeout to ensure store is updated before my side-effect action is dispatched.

from redux-subscriber.

ivantsov avatar ivantsov commented on May 28, 2024

Hi @itsdorosh,
Not sure if I understood your question. Can you provide an example with the code?

from redux-subscriber.

itsdorosh avatar itsdorosh commented on May 28, 2024

Sure.

subscribe("some.place.in.store", () => {
    // some code goes here
   store.dispatch(updateAnotherPlaceActions.update("some values"));
}

I understand, that update the same place in store can make recursive call of the callback function, but even if I update another part of store, callback is looped.

from redux-subscriber.

ivantsov avatar ivantsov commented on May 28, 2024

Hi @itsdorosh, so I investigated the issue and turned out there's a bug indeed. I'm gonna fix it asap, but can't promise anything really soon.

from redux-subscriber.

ivantsov avatar ivantsov commented on May 28, 2024

Hi @itsdorosh, I dived deeper into the problem and that's what I found/realized:

  1. You mentioned that Redux's subscribe allows dispatching actions inside the callback function. Which is true, however here it also says:

You may call dispatch() from a change listener, with the following caveats:
The listener should only call dispatch() either in response to user actions or under specific conditions (e. g. dispatching an action when the store has a specific field). Calling dispatch() without any conditions is technically possible, however it leads to an infinite loop as every dispatch() call usually triggers the listener again.

So, basically the issue you described for redux-subscribe also exists in Redux itself. And the correct solution is already proposed - use conditions.

  1. What you're doing in your example is dispatching an action as a reaction on another dispatch. Which I do think you should avoid, because it can be done within 1 action.

To summarize, redux-subscriber allows calling dispatch inside subscriber callback, but it might lead to an infinite loop. In order to avoid you should use conditions. And better choice would be not to call dispatch inside, because it means that you likely have a problem with your app/redux store architecture.

from redux-subscriber.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.