-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathNavComponent.tsx
More file actions
118 lines (113 loc) · 2.78 KB
/
NavComponent.tsx
File metadata and controls
118 lines (113 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
IonButton,
IonContent,
IonHeader,
IonLabel,
IonNav,
IonNavLink,
IonTitle,
IonToolbar,
IonButtons,
IonBackButton,
IonPage,
} from '@ionic/react';
import React, { useEffect, useRef } from 'react';
const PageOne = ({
nav,
...restOfProps
}: {
someString: string;
someNumber: number;
someBoolean: boolean;
nav: React.MutableRefObject<HTMLIonNavElement>;
}) => {
return (
<>
<IonHeader>
<IonToolbar>
<IonTitle>Page One</IonTitle>
<IonButtons slot="start">
<IonBackButton />
</IonButtons>
</IonToolbar>
</IonHeader>
<IonContent id="pageOneContent">
<IonLabel>Page one content</IonLabel>
<div id="pageOneProps">{JSON.stringify(restOfProps)}</div>
<div id="navRef">Nav ref is defined: {nav.current !== null ? 'true' : 'false'}</div>
<IonNavLink
routerDirection="forward"
component={PageTwo}
componentProps={{
someValue: 'Hello',
nav: nav,
}}
>
<IonButton>Go to Page Two</IonButton>
</IonNavLink>
</IonContent>
</>
);
};
const PageTwo = ({ nav, ...rest }: { someValue: string; nav: React.MutableRefObject<HTMLIonNavElement> }) => {
return (
<>
<IonHeader>
<IonToolbar>
<IonTitle>Page Two</IonTitle>
<IonButtons slot="start">
<IonBackButton />
</IonButtons>
</IonToolbar>
</IonHeader>
<IonContent id="pageTwoContent">
<IonLabel>Page two content</IonLabel>
<div id="pageTwoProps">{JSON.stringify(rest)}</div>
<IonNavLink routerDirection="forward" component={() => <PageThree nav={nav} />}>
<IonButton>Go to Page Three</IonButton>
</IonNavLink>
</IonContent>
</>
);
};
const PageThree = ({ nav }: { nav: React.MutableRefObject<HTMLIonNavElement> }) => {
useEffect(() => {
return () => {
window.dispatchEvent(new CustomEvent('pageThreeUnmounted'));
};
});
return (
<>
<IonHeader>
<IonToolbar>
<IonTitle>Page Three</IonTitle>
<IonButtons slot="start">
<IonBackButton />
</IonButtons>
</IonToolbar>
</IonHeader>
<IonContent id="pageThreeContent">
<IonLabel>Page three content</IonLabel>
<IonButton onClick={() => nav.current.popToRoot()}>popToRoot</IonButton>
</IonContent>
</>
);
};
const NavComponent: React.FC = () => {
const ref = useRef<any>(null);
return (
<IonPage>
<IonNav
ref={ref}
root={PageOne}
rootParams={{
someString: 'Hello',
someNumber: 3,
someBoolean: true,
nav: ref,
}}
/>
</IonPage>
);
};
export default NavComponent;