You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -154,8 +154,8 @@ export default function App() {
154
154
</Stack.Navigator>
155
155
</NavigationContainer>
156
156
);
157
+
// codeblock-focus-end
157
158
}
158
-
// codeblock-focus-end
159
159
160
160
functionHomeScreen() {
161
161
return<View />;
@@ -181,21 +181,33 @@ function SignUpScreen() {
181
181
</TabItem>
182
182
</Tabs>
183
183
184
-
When we define screens like this, when `isSignedIn` is`true`, React Navigation will only see the `Home`, `Profile` and `Settings` screens, and when it's`false`, React Navigation will see the `SignIn` and `SignUp` screens. This makes it impossible to navigate to the `Home`, `Profile` and `Settings` screens when the user is not signed in, and to `SignIn` and `SignUp` screens when the user is signed in.
184
+
When `useIsSignedIn` returns`true`, React Navigation will only see the `Home`, `Profile` and `Settings` screens, and when it returns`false`, React Navigation will see the `SignIn` and `SignUp` screens. This makes it impossible to navigate to the `Home`, `Profile` and `Settings` screens when the user is not signed in, and to `SignIn` and `SignUp` screens when the user is signed in.
185
185
186
186
This pattern has been in use by other routing libraries such as React Router for a long time, and is commonly known as "Protected routes". Here, our screens which need the user to be signed in are "protected" and cannot be navigated to by other means if the user is not signed in.
187
187
188
-
The magic happens when the value of the `isSignedIn` variable changes. Let's say, initially `isSignedIn` is`false`. This means, either `SignIn` or `SignUp` screens are shown. After the user signs in, the value of `isSignedIn` will change to `true`. React Navigation will see that the `SignIn` and `SignUp` screens are no longer defined and so it will remove them. Then it'll show the `Home` screen automatically because that's the first screen defined when `isSignedIn` is`true`.
188
+
The magic happens when the value returned by `useIsSignedin`changes. Let's say, initially `useIsSignedIn` returns`false`. This means, either `SignIn` or `SignUp` screens are shown. After the user signs in, the return value of `useIsSignedIn` will change to `true`. React Navigation will see that the `SignIn` and `SignUp` screens are no longer defined and so it will remove them. Then it'll show the `Home` screen automatically because that's the first screen defined when `useIsSignedIn` returns`true`.
189
189
190
190
The example shows stack navigator, but you can use the same approach with any navigator.
191
191
192
192
By conditionally defining different screens based on a variable, we can implement auth flow in a simple way that doesn't require additional logic to make sure that the correct screen is shown.
193
193
194
194
To do this, we need a couple of things:
195
195
196
+
<TabsgroupId="config"queryString="config">
197
+
<TabItemvalue="static"label="Static"default>
198
+
196
199
1. Define two hooks: `useIsSignedIn` and `useIsSignedOut`, which return a boolean value indicating whether the user is signed in or not.
197
200
2. Use the `useIsSignedIn` and `useIsSignedOut` along with the [`if`](static-configuration.md#if) property to define the screens that are available based on the condition.
198
201
202
+
</TabItem>
203
+
204
+
<TabItemvalue="dynamic"label="Dynamic">
205
+
206
+
1. Define two hooks: `useIsSignedIn` and `useIsSignedOut`, which return a boolean value indicating whether the user is signed in or not.
207
+
2. Use the `useIsSignedIn` and `useIsSignedOut` along with conditional rendering to define the screens that are available based on the condition.
208
+
209
+
</TabItem>
210
+
</Tabs>
199
211
This tells React Navigation to show specific screens based on the signed in status. When the signed in status changes, React Navigation will automatically show the appropriate screen.
0 commit comments