Skip to content

FluidGroup/swiftui-abstract-state

Repository files navigation

SwiftUI - AbstractState

A property wrapper that can be used to abstract the state and binding properties.

struct Counter: View {
  
  @AbstractState var count: Int
  
  init(initialValue: Int) {
    _count = AbstractState(initialValue)
  }
  
  init(binding: Binding<Int>) {
    _count = AbstractState(binding)
  }
  
  var body: some View {
    Button("Up \(count)") {
      count += 1
    }
  }
  
}
#Preview("State") {
  Counter(initialValue: 1)
}
struct Wrapper: View {
  
  @State private var count: Int = 1
  
  var body: some View {
    Counter(binding: $count)
  }
  
}

#Preview("Binding") {  
  Wrapper()
}

About

A property wrapper that abstracts the functionality of SwiftUI's @State and @binding

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages