Skip to content

Commit 22b42e2

Browse files
committed
Fixed README.
1 parent 061b1b5 commit 22b42e2

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,24 +280,30 @@ struct ChildView: View {
280280

281281
## NavigationStack injection
282282

283-
By default you can programmatically push and pop only inside the `NavigationStackView` hierarchy (by accessing the `NavigationStack` environment object). If you want to use the `NavigationStack` outside the `NavigationStackView` you need to create your own `NavigationStack` (wherever you want) **and pass it as parameter to the `NavigationStackView`**.
283+
By default you can programmatically push and pop only inside the `NavigationStackView` hierarchy (by accessing the `NavigationStack` environment object). If you want to use the `NavigationStack` outside the `NavigationStackView` you need to create your own `NavigationStack` (wherever you want) **and pass it as a parameter to the `NavigationStackView`**. This is useful when you want to decouple your routing logic from views.
284284

285285
**Important:** Every `NavigationStack` must be associated to a `NavigationStackView`. A `NavigationStack` cannot be shared between multiple `NavigationStackView`.
286286

287-
This is useful when you want to _decouple your routing logic from views by using your own router class_, for example:
287+
For example:
288288

289289
```
290+
struct RootView: View {
291+
let navigationStack: NavigationStack
292+
293+
var body: some View {
294+
NavigationStackView(navigationStack: navigationStack) {
295+
HomeScreen(router: MyRouter(navStack: navigationStack))
296+
}
297+
}
298+
}
299+
290300
class MyRouter {
291301
private let navStack: NavigationStack
292302
293303
init(navStack: NavigationStack) {
294304
self.navStack = navStack
295305
}
296306
297-
func rootView() -> some View {
298-
RootView()
299-
}
300-
301307
func toLogin() {
302308
self.navStack.push(LoginScreen())
303309
}
@@ -307,13 +313,18 @@ class MyRouter {
307313
}
308314
}
309315
310-
struct RootView: View {
311-
let navStack: NavigationStack
316+
struct HomeScreen: View {
312317
let router: MyRouter
313318
314319
var body: some View {
315-
NavigationStackView(navigationStack: navStack) {
316-
router.rootView()
320+
VStack {
321+
Text("Home")
322+
Button("To Login") {
323+
router.toLogin()
324+
}
325+
Button("To SignUp") {
326+
router.toSignUp()
327+
}
317328
}
318329
}
319330
}

0 commit comments

Comments
 (0)