Hello, I created simple view using TCA, but I cannot update the state within onAppear
function, the state is not changing so the view not updated is it prohibited to change state inside onAppear
?
Thanks before.
Text(viewStore.currentStep) // this should be updated every one second
.onAppear{
viewStore.send(.viewAppeared)
}
public func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case .viewAppeared:
state.isTimerOn = true
return EffectTask(value: .toggleTimer(true)) // -> not working
case .viewDisappeared:
return .cancel(id: CancellableTask.timer)
case .tickTock:
state.currentStep -= 1
if state.currentStep == 0 {
state.isTimerOn = false
return .cancel(id: CancellableTask.timer)
}
return .none
case .toggleTimer(let value):
state.isTimerOn = value
if state.isTimerOn {
return EffectTask(value: .startTimerTask)
} else {
return .cancel(id: CancellableTask.timer)
}
case .startTimerTask:
return .run { sender in
while !Task.isCancelled {
try await Task.sleep(nanoseconds: 1_000_000_000)
await sender(.tickTock)
}
}
.cancellable(id: CancellableTask.timer)
}
}