BindableState + SharedState strategy reseting binding #1661
-
Hi, I'm attempting to use a BindableState property inside a state that is working on a subset of a parent state. I am experiencing a compiled file, but when I change the property's value, it resets back to its original value. To provide more context, consider the 01-GettingStarted-SharedState case study. I added a simple BindableState Bool to the Profile State, added an action, BindingReducer, and toggle object. In this gif, I am repeatedly clicking the toggle button. This is the console output:
However, I am expecting state changes to the I'm 1 month into learning TCA and a few months into learning SwiftUI - And having a blast most of the time. I'm all ears to any hints or directions. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @derekc00, as you can see: struct SharedState: ReducerProtocol {
struct State: Equatable {
var counter = Counter.State()
var currentTab = Tab.counter
var profile: Profile.State {
get {
Profile.State(
currentTab: self.currentTab,
count: self.counter.count,
maxCount: self.counter.maxCount,
minCount: self.counter.minCount,
numberOfCounts: self.counter.numberOfCounts
)
}
set {
self.currentTab = newValue.currentTab
self.counter.count = newValue.count
self.counter.maxCount = newValue.maxCount
self.counter.minCount = newValue.minCount
self.counter.numberOfCounts = newValue.numberOfCounts
}
}
}
…
This means that the struct SharedState: ReducerProtocol {
struct State: Equatable {
var counter = Counter.State()
var currentTab = Tab.counter
+ var isOnStorage: Bool = false
var profile: Profile.State {
get {
Profile.State(
currentTab: self.currentTab,
count: self.counter.count,
maxCount: self.counter.maxCount,
minCount: self.counter.minCount,
numberOfCounts: self.counter.numberOfCounts,
+ isOn: self.isOnStorage
)
}
set {
self.currentTab = newValue.currentTab
self.counter.count = newValue.count
self.counter.maxCount = newValue.maxCount
self.counter.minCount = newValue.minCount
self.counter.numberOfCounts = newValue.numberOfCounts
+ self.isOnStorage = newValue.isOn
}
}
}
…
When there are many internal fields that you want to track, and when you're able to create a struct State: Equatable {
var counter = Counter.State()
var currentTab = Tab.counter
private var profileStorage = Profile.State()
var profile: Profile.State {
get {
var state = profileStorage
state.currentTab = self.currentTab
state.count = self.counter.count
state.maxCount = self.counter.maxCount
state.minCount = self.counter.minCount
state.numberOfCounts = self.counter.numberOfCounts
return state
}
set {
self.profileStorage = newValue
// Synchronize other properties
self.currentTab = newValue.currentTab
self.counter.count = newValue.count
self.counter.maxCount = newValue.maxCount
self.counter.minCount = newValue.minCount
self.counter.numberOfCounts = newValue.numberOfCounts
}
}
}
… In both cases, the state of the As an aside, I don't think that Footnotes
|
Beta Was this translation helpful? Give feedback.
Hey @derekc00, as you can see: