bcherny / undux

⚡️ Dead simple state for React. Now with Hooks support.

Home Page:https://undux.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Subscriptions are fired multiple times

tilman-tomorrow opened this issue · comments

I love undux and how easy it is to use! Thanks for creating it and maintaining it!
I just stumbled across an behavior that I felt a bit weird to me: When I subscribe to a store change via store.on('foo').subscribe(a => ...), the effect is fired not once, but lots of times: On the first change it fires once, on the second twice, on the third three times and so on. Is this intended?

Actual behavior
After a couple of store changes, each effect is fired up to hundreds of times (for 40 store changes the effects were fired 820 times)

Expected behavior
Each change should only fire an effect only once

Steps to reproduce
Here's an example: https://codesandbox.io/s/undux-demo-forked-tmb1o?file=/src/index.js:488-563 (watch the console.log)

commented

Temporary fix/workaround, you could try debounce function

Thanks for the hint @DeanKamali! I tried around with it, and realized that I was using the effects inside a React Component which looked it was reloaded every time the store changed.
Now I switched to using effects via export default createConnectedStore(initialState, withEffects); (https://undux.org/#quick-start/2), which I didn't see before. That works fine!