goatslacker / goatslacker/alt

ImmutableUtil store state is reset when dispatching initial action on store

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

Description

Hello,

I am experiencing an issue where my store is getting reset back to its default state (as defined in the constructor) when firing an initial action on the client. The store is using the `ImmutableUtil` decorator, and upon inspecting the initial bootstrapped data from the server, everything is fine. However, when I dispatch an action on the client, the state is reset. It's worth mentioning that I am not even attempting yet to change the store's state using `this.setState()`.

Component method firing the action:

``` javascript
onSearchInputChange(evt) {
require('debug')('dev')('onSearchInputChange', this.context.flux.stores.numberAllocations.state.toString());

this.context.flux.getActions('numberAllocations')
.updateFilter({ key: 'dialledNumber', value: evt.target.value });
}
```

The store:

``` javascript
import Immutable from 'immutable';

import immutableStore from 'alt/utils/ImmutableUtil';
import _trimLeft from 'lodash/string/trimLeft';

@immutableStore
export default class NumberAllocationsStore {
constructor() {
this.bindActions(this.alt.getActions('numberAllocations'));

this.state = new Immutable.Map({
data: new Immutable.List([]),
meta: new Immutable.Map({}),
filters: new Immutable.Map({
dialledNumber: ''
}),
dataCurrentAllocation: new Immutable.Map({})
});
}

onRetrieveAllocations({ data, meta }) {
this.setState(this.state.merge({ data, meta }));
}

onRetrieveAllocation(data) {
this.setState(this.state.set('dataCurrentAllocation', data));
}

onUpdateFilter({ key, value }) {
require('debug')('dev')('onUpdateFilter', key, value, this.state.toString());
}

}
```

To demonstrate this - the console output in `onSearchInputChange` (ran just before dispatching the action) is fine, while the console output for `onUpdateFilter` returns the unwanted reset store state:

![3df59882-8f90-11e5-8833-8c2b5c8a54f0](https://cloud.githubusercontent.com/assets/5153746/11318548/431443f8-904f-11e5-93dc-68126edbc3ae.jpg)

I'm really quite stuck with this one. :confused: Alt version is 0.17.8. Any help would be greatly appreciated!

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.