goatslacker / goatslacker/alt

Add finer control for store state with events

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

Description

Hi,

I currently have a number of stores which contain state across functional areas of my application. For example, I have one store to deal with all state regarding users. I maintain the state of the currently selected user, the list of all users, any filter that a user has applied, etc...

```
constructor () {
...
this.state = {
userList: [],
currentUser: null,
userFilter: '',
error: 0,
}
}
```

Using the store in this way saves a good amount of boilerplate code. I don't have to have a separate store for each one of the variables in the UserStore's state. I can also reuse functions in the UserStore for each of these items.

The downside to this approach is that the UserStore's change events are overly generic. I may update just one attribute of the state in a given function...

```
userLoaded (userData) {
this.setState({currentUser: userData});
}
```

But then the change event would fire callbacks on components that don't necessarily care about the currentUser. They may be waiting for an update of the userFilter or errors.

Would it be possible to add some type of extra change events so that I could fire events specific to particular pieces of the state? In this case, I would want to trigger a "UserLoaded" change event.

Or should I just separate my state into separate stores? Perhaps there is another way?

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.