Skip to content

Commit f7c0198

Browse files
committed
Make static and dynamic examples match better and change text to better reflect code
1 parent ee18a71 commit f7c0198

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

versioned_docs/version-7.x/auth-flow.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const useIsSignedIn = () => {
4646
};
4747

4848
const useIsSignedOut = () => {
49-
return false;
49+
return !useIsSignedIn();
5050
};
5151

5252
// codeblock-focus-start
@@ -83,13 +83,13 @@ const RootStack = createNativeStackNavigator({
8383
},
8484
},
8585
});
86+
// codeblock-focus-end
8687

8788
const Navigation = createStaticNavigation(RootStack);
8889

8990
export default function App() {
9091
return <Navigation />;
9192
}
92-
// codeblock-focus-end
9393

9494
function HomeScreen() {
9595
return <View />;
@@ -129,11 +129,11 @@ const useIsSignedIn = () => {
129129
};
130130

131131
const useIsSignedOut = () => {
132-
return false;
132+
return !useIsSignedIn();
133133
};
134134

135-
// codeblock-focus-start
136135
export default function App() {
136+
// codeblock-focus-start
137137
const isSignedIn = useIsSignedIn();
138138

139139
return (
@@ -154,8 +154,8 @@ export default function App() {
154154
</Stack.Navigator>
155155
</NavigationContainer>
156156
);
157+
// codeblock-focus-end
157158
}
158-
// codeblock-focus-end
159159

160160
function HomeScreen() {
161161
return <View />;
@@ -181,21 +181,33 @@ function SignUpScreen() {
181181
</TabItem>
182182
</Tabs>
183183

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.
185185

186186
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.
187187

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`.
189189

190190
The example shows stack navigator, but you can use the same approach with any navigator.
191191

192192
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.
193193

194194
To do this, we need a couple of things:
195195

196+
<Tabs groupId="config" queryString="config">
197+
<TabItem value="static" label="Static" default>
198+
196199
1. Define two hooks: `useIsSignedIn` and `useIsSignedOut`, which return a boolean value indicating whether the user is signed in or not.
197200
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.
198201

202+
</TabItem>
203+
204+
<TabItem value="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>
199211
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.
200212

201213
## Define the hooks

0 commit comments

Comments
 (0)