Add finer control for store state with events
- Lingua principale
- JavaScript
- Stelle
- 3.4k
- Fork
- 312
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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?
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Non sono stati identificati file o test. Inizia tracciando il modo in cui UserStore.setState emette eventi di modifica e il modo in cui i componenti si sottoscrivono, quindi confronta gli eventi con ambito con store separati. Per considerare il lavoro completato sarebbe necessario un design approvato da un maintainer per notifiche di modifica specifiche dello stato, oppure un’alternativa, con il relativo comportamento definito e testato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript
- Ambito
- frontend
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100