Allow setState to be overridden
- Dominant language
- JavaScript
- Stars
- 3.4k
- Forks
- 312
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to implement Store level persistency for my application (demo app for a book). The idea is that I restore Store state at constructor and save it whenever it changes. I noticed `this.dispatcher.register` gets fairly close but unfortunately it's called before state change is applied making it unsuitable for this purpose. As a next step I tried to override `setState` but this isn't possible due to the current class hierarchy. I believe it would be a good idea to allow this.
Just for the record I ended up "solving" the problem like this:
``` javascript
class NoteStore {
constructor() {
this.bindActions(NoteActions);
this.exportPublicMethods({
persist: () => storage.set('notes', this.notes)
});
const initialData = storage.get('notes');
this.notes = Array.isArray(initialData) ? initialData : [];
}
...
}
```
I hit `NoteStore.persist()` at my store listener. That will trigger the persistency logic.
It is possible there's a simpler way to achieve the same in the current system but I failed to find that. Ideally all of this logic belongs to store level no matter what the solution is.
Contributor guide
Assessment
This issue has not been assessed yet.