Yes, that seems to be part of the problem. If I add .tag(1), .tag(2)... I get the correct index to the reducer, but the content is still missing. The text "Lots of users" does not display.
Thanks for the help. It seems like I have done something else wrong. The component is nested under a login page, with a pullback on the reducer
IfLetStore(self.store.scope(state: \.loggedInApplication, action: RootAction.loggedInApplication)) { store in
MainView(store: store)
}
If I change it to be standalone, it works as soon I added the tags:
IfLetStore(self.store.scope(state: \.loggedInApplication, action: RootAction.loggedInApplication)) { store in
MainView(store: Store(
initialState: MainViewState(meUser: viewStore.meUser!),
reducer: mainViewReducer,
environment: MainViewEnvironment(mainQueue: DispatchQueue.main.eraseToAnyScheduler()))
)
}
I think that the composable architecture framework looks really nice, but I have difficulties to understand how to distribute shared state in a clean way. For example how to share a logged in user object among several sub pages.
@aarsland have you seen the TicTacToe Example? It's quite wonderful, and may help you start with the initial composition, with some parent (Global State) component that manages other child components, and then:
You could evolve that example with a computed property that gets some state from your parent component, like this:
struct GlobalState {
var some = ""
// See here
var localState: LocalState {
get {
LocalState(some: some)
}
set {
some = newValue.some
}
}
}
enum GlobalAction {
case localView(LocalAction)
}
Then when presenting the local view, any changes made to that localStore will be propagated back to the Global Store, retaining those changes in the Global State.
And vice-versa.. Changes happening in the Global State will propagate to the Local, scoped state.
Thank you for your answer! Yes, I have seen that example and it looks ok, but what if the navigation is more complex - like:
If there is 3 screens:
A - A login page where the user object is set.
B - A page under A that shows a list of some sumaries of something - this page do not need the user that is set in A.
C - A page that shows some detailes for an element from the list in page B. This page needs the user object.
Should then screen B also have the user data to pass it on to C?