Creating a custom environment key works, but if I wish to set the value in a view, XCODE doesn't allow it. But the predefined environment values can be set. What am I doing wrong?
struct ResetDefault: EnvironmentKey {
static var defaultValue: Bool = false
}
extension EnvironmentValues {
var resetDefault: Bool {
get { self[ResetDefault.self] }
set { self[ResetDefault.self] = newValue }
}
}
struct ResetView: View {
@Environment(\.resetDefault) var reset
var body: some View {
Text("Reset").onAppear() {
reset = true. // Cannot assign to property: 'reset' is a get-only property
}
}
}
The Environment
is used to pass values in parent > child
direction, so value is set for usage. If you want to change internal of environment value then you need to wrap it somehow, possible variants are binding or reference type holder.
Here is an example of usage based on binding (similar to how .editMode and .presentationMode work)
struct TestResetEnv: View {
@State private var isActive = false
@State private var reset = false
var body: some View {
VStack {
Text("Current: \(reset ? "true" : "false")")
Button("Go") { self.isActive.toggle() }
if isActive {
ResetView()
}
}.environment(\.resetDefault, $reset) // set for children as env!!
}
}
struct ResetDefault: EnvironmentKey {
static var defaultValue: Binding<Bool> = .constant(false)
}
extension EnvironmentValues {
var resetDefault: Binding<Bool> {
get { self[ResetDefault.self] }
set { self[ResetDefault.self] = newValue }
}
}
struct ResetView: View {
@Environment(\.resetDefault) var reset
var body: some View {
Text("Reset").onAppear() {
self.reset.wrappedValue.toggle() // << change wrapped !!
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With