swift
struct MyComponent: View {
var displayString: String
var body: some View {
Text(displayString)
}
}
struct MyComponent: View {
var displayString: String
var body: some View {
Text(displayString)
}
}
struct ConditionalComponent: View {
let condition: Bool
var body: some View {
Group {
if condition {
Text("Condition is true")
} else {
Text("Condition is false")
}
}
}
}
// Usage
ConditionalComponent(condition: true)
struct Parent: View {
let data: String
var body: some View {
IntermediateComponent(data: data)
}
}
struct IntermediateComponent: View {
let data: String
var body: some View {
ChildComponent(data: data)
}
}
struct ChildComponent: View {
let data: String
var body: some View {
Text("Received data: \(data)")
}
}
// Usage
Parent(data: "Some data")
struct ClickableComponent: View {
@State private var clicked = false
var body: some View {
Button(action: {
clicked = true
}) {
Text(clicked ? "Button clicked" : "Click me")
}
}
}
struct TextInputComponent: View {
@State private var text = ""
var body: some View {
TextField("Enter text", text: $text)
}
}
struct ExampleComponent: View {
var body: some View {
Text("Hello, World!")
}
}
struct ExampleComponent_Previews: PreviewProvider {
static var previews: some View {
ExampleComponent()
}
}
struct ListComponent: View {
let items: [String]
var body: some View {
List(items, id: \.self) { item in
Text(item)
}
}
}
// Usage
let items = ["Item 1", "Item 2", "Item 3"]
ListComponent(items: items)
struct Person: Identifiable {
let name: String
let age: Int
let id: String
}
struct ItemKeysExample: View {
let items: [Person]
var body: some View {
List(items) { person in
Text("Name: \(person.name), Age: \(person.age)")
}
}
}
// Usage
ItemKeysExample(items: [Person(name: "John", age: 30, id: "1"), Person(name: "Jane", age: 28, id: "2"), Person(name: "Bob", age: 25, id: "3")])
struct Parent<Header: View, Content: View>: View {
let header: Header
let content: Content
var body: some View {
VStack {
header
content
}
}
}
// Usage
Parent(
header: Text("Header"),
content: Child()
)
struct Child: View {
var body: some View {
Text("Child Content")
}
}
struct ModifiersExample: View {
var body: some View {
Text("Hello, World!")
.padding(EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16))
.background(Color.blue)
}
}
struct Counter: View {
@State private var count = 0
var body: some View {
Button(action: {
count += 1
}) {
Text("Count: \(count)")
}
}
}
struct CustomEnvironmentKey: EnvironmentKey {
static let defaultValue: String = ""
}
extension EnvironmentValues {
var customData: String {
get { self[CustomEnvironmentKey.self] }
set { self[CustomEnvironmentKey.self] = newValue }
}
}
struct Parent: View {
let data: String
var body: some View {
Intermediate().environment(\.customData, data)
}
}
struct Intermediate: View {
var body: some View {
Child()
}
}
struct Child: View {
@Environment(\.customData) private var data
var body: some View {
Text("Received data: \(data)")
}
}
// Usage
Parent(data: "Some data")
struct SideEffectOnLoadComponent: View {
@State private var hasPerformedSideEffect = false
var body: some View {
if !hasPerformedSideEffect {
DispatchQueue.main.async {
// Perform side effect, e.g. fetch data, update external data source
hasPerformedSideEffect = true
}
}
// Other UI components
Text("Hello, World!")
}
}