Reswift: Accessing state outside of action or reducer

Created on 23 Mar 2016  路  3Comments  路  Source: ReSwift/ReSwift

Since we have a global store that is accessible anywhere, is it safe to use it as needed? Sometime outside of ReSwift we might want to check on the current state.

var myState = store.state.foo

or should all of this done inside actions / action creators?

Most helpful comment

@eschan Another important gotcha to be aware of is that you should only ever access the Store from the main thread. ReSwift operates under the assumption that all actions are being dispatched on the main thread and that all accesses to the store happen there as well.

I plan on encoding that in future, for now developers need to keep that in mind themselves.

All 3 comments

For adhoc state querying, it's perfectly fine to access the store. You will be able to retrieve the state at any point in time without creating strong references to state. The alternative being subscribing to the state updates and mapping changes into a local variable. Keep it simple first and see where you run into issues.
Querying of state like that is done a fair bit in Middleware too.

I think the only warning I would give is to watch out actual globals (ie, shared instances). I'm guessing though you are doing it the right way, and by 'global store' you are referring to the store only being instantiated once and then being injected around the app.

@eschan Another important gotcha to be aware of is that you should only ever access the Store from the main thread. ReSwift operates under the assumption that all actions are being dispatched on the main thread and that all accesses to the store happen there as well.

I plan on encoding that in future, for now developers need to keep that in mind themselves.

Seems like we've resolved this issue! Closing it out.

Was this page helpful?
0 / 5 - 0 ratings