Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created March 28, 2022 07:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/440dbdbe9a5fa21ff5439b5f42582a44 to your computer and use it in GitHub Desktop.
Save chriseidhof/440dbdbe9a5fa21ff5439b5f42582a44 to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// Shared
//
// Created by Chris Eidhof on 28.03.22.
//
import SwiftUI
struct Item: View {
var id: Int
@State private var counter = 0
var body: some View {
VStack {
Text("Item \(id)")
Button("Counter: \(counter)") {
counter += 1
}
}
}
}
struct ItemWrapper: View {
var id: Int
var body: some View {
ZStack {
Item(id: id)
}
}
}
final class MyObject: ObservableObject {
init() { print("Allocating") }
}
struct StateObjectItemTest: View {
@StateObject private var object = MyObject()
let body = Text("Hello")
}
struct StateItemTest: View {
@State var item: Int = {
print("Initing")
return 0
}()
let body = Text("Hello")
}
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink("State Objects") {
List(0..<1000) { id in
StateObjectItemTest()
}
}
NavigationLink("State Values") {
List(0..<1000) { id in
StateItemTest()
}
}
NavigationLink("Indirect State") {
List(0..<1000) { id in
ItemWrapper(id: id)
}
}
NavigationLink("Direct State") {
List(0..<1000) { id in
Item(id: id)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment