goatslacker / goatslacker/alt

Are calls to setState really batched?

Open
#677 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.4k
Forks
312
PR merge metrics
No merged PRs in 30d

Description

The doc says

> For convenience, calls to setState are automatically batched as they are in React and only one change event will be emitted once the action finishes dispatching".

But from what I've seen, it doesn't seem to behave like that.

The following is the debug log of one dispatching process of https://github.com/iam4x/isomorphic-flux-boilerplate.

```
-----start dispatching-----
callbackId ID_1
callbackId ID_2
callbackId ID_3
handleDispatch
doSetState false
result of handleDispatch true
emitChange
callbackId ID_4
callbackId ID_5
callbackId ID_6
doSetState false
emitChange
callbackId ID_7
-----stop dispatching-----
```

As you can see, [`doSetState`](https://github.com/goatslacker/alt/blob/master/src/store/index.js#L6) does its work. It won't `emitChange` if it's still dispatching.

However the result of [`handleDispatch`](https://github.com/goatslacker/alt/blob/master/src/store/AltStore.js#L58-L70) is always true. Then `emitChange` is called immediately after while it's still dispatching.

As a result of it, we can't dispatch actions within component lifecycle methods like componentDidUpdate. Otherwise, we'll get error "Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch."

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.