Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 6877649

Browse files
committed
Update README
1 parent 33360bc commit 6877649

1 file changed

Lines changed: 23 additions & 27 deletions

File tree

README.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,55 @@ Resolve SwiftUI views trough a resolver injected in parent that is inspired by N
44

55
## Usage
66

7-
### Destinations with the `ResolvableDestination` protocol
7+
### Destinations with `@ViewBuilder` closures
88

9-
1. Implement a custom type that adopts the `ResolvableDestination` protocol
10-
2. Provide a value of this type in the parent view with the `destination` view modifier
11-
3. Resolve the view using `DestinationView` with the initializer that takes the destination type as the first argument
9+
This is the most basic way to use Destinations.
10+
11+
1. Register a `@ViewBuilder` closure for a value type with the `destination(for:)` view modifier
12+
2. In a child view, resolve the destination by using `DestinationView` with a matching value type
1213

1314
```swift
1415
import Destinations
1516

16-
struct MyDestination: ResolvableDestination {
17-
let base: Int
18-
19-
func body(value: Int) -> some View {
20-
Text(base + value, format: .number)
21-
}
22-
}
23-
2417
struct ParentView: View {
2518
var body: some View {
2619
ChildView()
27-
.destination(MyDestination(base: 100))
20+
.destination(for: Int.self) { value in
21+
Text(100 + value, format: .number)
22+
}
2823
}
2924
}
3025

3126
struct ChildView: View {
3227
var body: some View {
33-
DestinationView(MyDestination.self, value: 1)
28+
DestinationView(value: 1)
3429
}
3530
}
3631
```
3732

38-
### Destinations with `@ViewBuilder` closures
39-
40-
Destinations comes with one special destination that you can use to resolve any Hashable value without implementing a custom `ResolvableDestination` type.
33+
### Destinations with the `ResolvableDestination` protocol
4134

42-
1. Provide a `@ViewBuilder` closure for a value type in the parent view with the `destination(for:)` view modifier
43-
2. Resolve the view using `DestinationView` with the initializer with the value as the only argument
35+
Alternatively, you can register a custom `ResolvableDestination` implementation. This way you can inject properties from the parent and hold some state with SwiftUI property wrappers.
4436

45-
Note, however, that while this approach more closely resembles NavigationStack and is easier to use, it has one significant one significant trade-off. Like NavigationStack, it uses `AnyView` inside to erase the type information of the view that you provide in the `destination` closure, which is an unavoidable syntax limitation. In practical terms, this is may be less efficient when SwiftUI needs to detect changes between view updates.
37+
1. Implement a custom type that adopts the `ResolvableDestination` protocol
38+
2. Register a value of this type with the `destination` view modifier
39+
3. In a child view, resolve the view by using `DestinationView` with a value type that matches the value type used in step 1
4640

4741
```swift
4842
import Destinations
4943

44+
struct MyDestination: ResolvableDestination {
45+
let base: Int
46+
47+
func body(value: Int) -> some View {
48+
Text(base + value, format: .number)
49+
}
50+
}
51+
5052
struct ParentView: View {
5153
var body: some View {
5254
ChildView()
53-
.destination(for: Int.self) { value in
54-
Text(100 + value, format: .number)
55-
}
55+
.destination(MyDestination(base: 100))
5656
}
5757
}
5858

@@ -84,7 +84,6 @@ struct ContentView: View {
8484
List {
8585
DestinationNavigationLink(
8686
"Present MyDestination with push transition",
87-
destination: MyDestination.self,
8887
value: "Hello, world!"
8988
)
9089

@@ -95,7 +94,6 @@ struct ContentView: View {
9594
}
9695
.sheet(
9796
isPresented: $isSheetPresented,
98-
destination: MyDestination.self,
9997
value: "Hello, modal world!"
10098
)
10199
.destination(MyDestination())
@@ -134,6 +132,4 @@ struct FileSizeLabelDestination: ResolvableDestination {
134132
}
135133
}
136134
}
137-
138135
```
139-

0 commit comments

Comments
 (0)