Search code examples
fluxredux

Who should own the state value of an item?


My flux app displays a list locations. When user clicks on a location, it becomes 'active'. There can be only one active location at a time.

Should the state of location be a property of the location itself (e.g. location.active) or should it be part of the global state, e.g. activeLocationId?


Solution

  • I have written several variations of the app. It became obvious that active is not a property of the item itself (location in this case), but is the property of the UI (activeLocationId).

    Some benefits:

    • Single source of truth (You will not end up accidentally with two active locations).
    • It is not affected if new location list is loaded (This is regardless of wether the new list includes the active location or not).
    • Makes it easy to find the active location ID.