Passing Shared property to child stores #3414
Replies: 1 comment 1 reply
-
Hi @holzerryan You can find information about the shared property here. As you can see, I would suggest considering how to create a new initializer in AppFeature.State instead of relying on the compiler-provided initializer. When the store for the reducer is created, the Shared property is passed as a parameter from InsightsApp, but this method doesn’t actually retain the Shared instance. @main
struct InsightsApp: App {
// ✅
@Shared var featureFlagData
@Bindable var store: StoreOf<AppFeature>
init() {
// ✅ init shared Property
_featureFlagData = .init(.init())
store = Store(
initialState: AppFeature.State(
featureFlagData: Shared(FeatureFlagData())
)
) {
AppFeature()
}
store.send(.initializeFeatureFlags)
}
} @Reducer
struct AppFeature {
@ObservableState
struct State: Equatable {
// var path = StackState<Path.State>()
@Shared var featureFlagData: FeatureFlagData
var loggedIn: Bool = false
var login = LoginFeature.State.init()
var authenticated = AuthenticatedFeature.State.init(featureFlagData: Shared(FeatureFlagData()))
init(featureFlagData: Shared<FeatureFlagData>) {
_featureFlagData = featureFlagData
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I'm struggling with setting up a shared parameter that is initialized at the root level of the app and then passed to all of the child features. My root feature is very simple for now...The state has "loggedIn" and "featureFlagData". At the root, I want to define all feature flags that will be used in the app. I also want to determine if the login screen or the app is displayed. I get stuck when trying to pass the featureFlagData shared property to the authenticated view because I have a TabView and I cannot figure out how to pass the Shared(FeatureFlagData) in as the Initial State.
Then in my AppView, I have this:
How would I pass the AppFeature.state.featureFlagData to the initial state of the Authenticated and Login features?
Beta Was this translation helpful? Give feedback.
All reactions