Skip to content

Commit fed5edd

Browse files
committed
Update Part 6
1 parent b11ffca commit fed5edd

File tree

2 files changed

+380
-298
lines changed

2 files changed

+380
-298
lines changed

docs/tutorials/essentials/part-4-using-data.md

+28-8
Original file line numberDiff line numberDiff line change
@@ -1186,11 +1186,14 @@ import { PostsMainPage } from './features/posts/PostsMainPage'
11861186
import { SinglePostPage } from './features/posts/SinglePostPage'
11871187
import { EditPostForm } from './features/posts/EditPostForm'
11881188

1189+
// highlight-next-line
1190+
import { selectCurrentUsername } from './features/auth/authSlice'
1191+
11891192
// highlight-start
11901193
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
1191-
const user = useAppSelector(state => state.auth.username)
1194+
const username = useAppSelector(selectCurrentUsername)
11921195

1193-
if (!user) {
1196+
if (!username) {
11941197
return <Navigate to="/" replace />
11951198
}
11961199

@@ -1237,27 +1240,44 @@ We should now see both sides of the auth behavior working:
12371240
12381241
Since we now know who is logged in while using the app, we can show the user's actual name in the navbar. We should also give them a way to log out as well, by adding a "Log Out" button.
12391242
1240-
As with the other features we've built, we'll select the relevant state (the username and corresponding user object) from the store, display the values, and dispatch the `userLoggedOut()` action when they click the "Log Out" button:
1243+
We need to get the current user object from the store so we can read `user.name` for display. We can do that by first getting the current username from the auth slice, then using that to look up the right user object. This seems like a thing we might want to do in a few places, so this is a good time to write it as a reusable `selectCurrentUser` selector. We can put that in `usersSlice.ts`, but have it import and rely on the `selectCurrentUsername` from `authSlice.ts`:
1244+
1245+
```ts title="features/users/usersSlice.ts"
1246+
// highlight-next-line
1247+
import { selectCurrentUsername } from '@/features/auth/authSlice'
1248+
1249+
// omit the rest of the slice and selectors
1250+
1251+
// highlight-start
1252+
export const selectCurrentUser = (state: RootState) => {
1253+
const currentUsername = selectCurrentUsername(state)
1254+
return selectUserById(state, currentUsername)
1255+
}
1256+
// highlight-end
1257+
```
1258+
1259+
It's often useful to compose selectors together and use one selector inside of another. In this case, we can use both `selectCurrentUsername` and `selectUserById` together.
1260+
1261+
As with the other features we've built, we'll select the relevant state (the current user object) from the store, display the values, and dispatch the `userLoggedOut()` action when they click the "Log Out" button:
12411262
12421263
```tsx title="components/Navbar.tsx"
12431264
import { Link } from 'react-router-dom'
12441265

12451266
// highlight-start
12461267
import { useAppDispatch, useAppSelector } from '@/app/hooks'
12471268

1248-
import { selectCurrentUsername, userLoggedOut } from '@/features/auth/authSlice'
1249-
import { selectUserById } from '@/features/users/usersSlice'
1269+
import { userLoggedOut } from '@/features/auth/authSlice'
1270+
import { selectCurrentUser } from '@/features/users/usersSlice'
12501271

12511272
import { UserIcon } from './UserIcon'
12521273
// highlight-end
12531274

12541275
export const Navbar = () => {
12551276
// highlight-start
12561277
const dispatch = useAppDispatch()
1257-
const username = useAppSelector(selectCurrentUsername)
1258-
const user = useAppSelector(state => selectUserById(state, username))
1278+
const user = useAppSelector(selectCurrentUser)
12591279

1260-
const isLoggedIn = !!username && !!user
1280+
const isLoggedIn = !!user
12611281

12621282
let navContent: React.ReactNode = null
12631283

0 commit comments

Comments
 (0)