Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active March 1, 2022 22:23
Show Gist options
  • Select an option

  • Save mbrandonw/dee2ceac2c316a1619cfdf1dc7945f66 to your computer and use it in GitHub Desktop.

Select an option

Save mbrandonw/dee2ceac2c316a1619cfdf1dc7945f66 to your computer and use it in GitHub Desktop.

FB7673786

Paste the code below into a playground, and try incrementing and decrementing the stepper. The buttons will sporadically enable and disable.

This only happens with the Stepper.init(onIncrement:onDecrement:) initializer. It will work fine with a binding.

import SwiftUI
import PlaygroundSupport

class ViewModel: ObservableObject {
  @Published var count = 0
}

struct ViewModelView<Content: View>: View {
  @ObservedObject var viewModel = ViewModel()

  let content: (ViewModel) -> Content

  init(@ViewBuilder content: @escaping (ViewModel) -> Content) {
    self.content = content
  }

  var body: some View {
    self.content(self.viewModel)
  }
}

let v = ViewModelView { viewModel in
  Stepper(onIncrement: { viewModel.count += 1 }, onDecrement: { viewModel.count -= 1 }) {
    Text("\(viewModel.count)")
  }
}

PlaygroundPage.current.setLiveView(v)
@grigorye
Copy link

grigorye commented Mar 1, 2022

I probably can not reproduce the problem here (tried both macOS and iOS playgrounds). What I can reliably reproduce is Stepper, that does not trigger actions properly, on changing the direction, i.e. if I click "+", "+", and then "-", "-", "-", first "-" will act as "+", second "-" will not trigger any action, and only third "-" will act as "-" (that thing is easily reproducible without viewModel/TCA), via the following fragment:

let v = Stepper(
    onIncrement: { print("onIncrement") },
    onDecrement: { print("onDecrement") }
) {
    Text("XXX")
}

PlaygroundPage.current.setLiveView(v)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment