Skip to content

Instantly share code, notes, and snippets.

Last active October 25, 2023 09:34
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A container that makes implementing stateful previews in SwiftUI easier
struct StatefulPreviewContainer<Value, Content: View>: View {
@State var value: Value
var content: (Binding<Value>) -> Content
var body: some View {
init(_ value: Value, content: @escaping (Binding<Value>) -> Content) {
self._value = State(wrappedValue: value)
self.content = content
// Here is how to use the stateful preview container
struct TodoRowView_Previews_withGenericWrapper: PreviewProvider {
static var previews: some View {
StatefulPreviewContainer(Todo.sampple) { binding in
TodoRowView(todo: binding)
// Everything below: views and data model used in the above code snippet
struct Todo: Identifiable {
var id = UUID().uuidString
var title: String
var completed: Bool
extension Todo {
static var samples = [
Todo(title: "Write sample code", completed: false),
Todo(title: "Draft article", completed: false),
Todo(title: "???", completed: false),
Todo(title: "PROFIT!!!", completed: false)
static var sampple = Self.samples[0]
struct TodoRowView: View {
@Binding var todo: Todo
var body: some View {
Toggle(isOn: $todo.completed) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment