This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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