Comments (5)
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.
Hi @itsdorosh,
Not sure if I understood your question. Can you provide an example with the code?
from redux-subscriber.
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.
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.
Hi @itsdorosh, I dived deeper into the problem and that's what I found/realized:
- 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 calldispatch()
either in response to user actions or under specific conditions (e. g. dispatching an action when the store has a specific field). Callingdispatch()
without any conditions is technically possible, however it leads to an infinite loop as everydispatch()
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.
- 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)
- `initSubscriber` returning `subscribe` api (?) HOT 3
- Dispatch actions on subscription callback HOT 1
- Subscribe handler being fired twice for one change HOT 4
- [Question]: TypeScript types? HOT 5
- Consider using object-path-get HOT 1
- subscribing to boolean variables HOT 4
- Wired behavior with setState in sub HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-subscriber.