Skip to content

Commit 4b6c458

Browse files
committed
small fixes
1 parent f504396 commit 4b6c458

File tree

6 files changed

+54
-65
lines changed

6 files changed

+54
-65
lines changed

Diff for: react/advanced-hooks/03-transitions/practice/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useTransition, useDeferredValue } from 'react'
1+
import { useState, useTransition } from 'react'
22

33
export function App() {
44
const [tabIndex, setTabIndex] = useState(0)

Diff for: react/advanced-hooks/03-transitions/practice/GUIDE.md

-24
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22

33
This practice has three tabs where the rendering of the second tab is significantly slower than the others. If you click on the second tab and then immediately click to one the fast tabs you'll have to wait until the slow tab finishes first before you'll see your second clicked tab show.
44

5-
Decide if `useTransition` or `useDeferredValue` is better suited to make this UI faster
6-
7-
Here is the syntax for each:
8-
9-
```js
10-
/****************************************
11-
useTransition
12-
*****************************************/
13-
const [pending, startTransition] = useTransition()
14-
15-
function someEvent() {
16-
setState() // high priority
17-
startTransition(() => {
18-
setState() // low priority
19-
})
20-
}
21-
22-
/****************************************
23-
useDeferredValue
24-
*****************************************/
25-
const [myState, setMyState] = useState()
26-
const myStateDeferred = useDeferredValue(myState)
27-
```
28-
295
## Task 1
306

317
Make the UI faster by allowing the user to click on the slow tab then immediately click on a fast tab. Clicking the fast tab should show the fast tab soon instead of waiting for the slow tab to finish.
+42-39
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,17 @@
11
import { useOptimistic, useState, useTransition } from 'react'
22
import { type ResponseData, updateDatabase } from './helpers/mockServer'
33

4-
// export function App() {
5-
// const [error, setError] = useState('')
6-
// const [likes, setLikes] = useState(0)
7-
8-
// async function submit(e: React.FormEvent) {
9-
// e.preventDefault()
10-
11-
// const data = (await updateDatabase(likes + 1).then((r) => r.json())) as ResponseData
12-
13-
// if (!data.error) {
14-
// console.log(data.likes)
15-
// setLikes(data.likes)
16-
// } else {
17-
// setLikes(data.likes)
18-
// setError(data.error)
19-
// }
20-
// }
4+
// Remember that useOptimistic will not work with <form onSubmit />, and requires
5+
// <form action />
216

22-
// return (
23-
// <form onSubmit={submit} className="space-y-6">
24-
// <div>
25-
// <button type="submit" className="button text-xl">
26-
// Like My Post: {likes}
27-
// </button>
28-
// </div>
29-
// {error && <div className="text-red-800">{error}</div>}
30-
// </form>
31-
// )
32-
// }
33-
34-
// // Step Two: home-made optimistic UI with transitions
357
export function App() {
368
const [error, setError] = useState('')
379
const [likes, setLikes] = useState(0)
3810

39-
const [opLikes, setOpLikes] = useState(0)
40-
const [pending, startTransition] = useTransition()
41-
4211
async function submit(e: React.FormEvent) {
4312
e.preventDefault()
4413

45-
startTransition(() => {
46-
setOpLikes(opLikes + 1)
47-
})
48-
49-
const data = (await updateDatabase(opLikes + 1).then((r) => r.json())) as ResponseData
14+
const data = (await updateDatabase(likes + 1).then((r) => r.json())) as ResponseData
5015

5116
if (!data.error) {
5217
console.log(data.likes)
@@ -61,10 +26,48 @@ export function App() {
6126
<form onSubmit={submit} className="space-y-6">
6227
<div>
6328
<button type="submit" className="button text-xl">
64-
Like My Post: {opLikes}
29+
Like My Post: {likes}
6530
</button>
6631
</div>
6732
{error && <div className="text-red-800">{error}</div>}
6833
</form>
6934
)
7035
}
36+
37+
// // Step Two: home-made optimistic UI with transitions
38+
// export function App() {
39+
// const [error, setError] = useState('')
40+
// const [likes, setLikes] = useState(0)
41+
42+
// const [opLikes, setOpLikes] = useState(0)
43+
// const [pending, startTransition] = useTransition()
44+
45+
// async function submit(e: React.FormEvent) {
46+
// e.preventDefault()
47+
48+
// startTransition(() => {
49+
// setOpLikes(opLikes + 1)
50+
// })
51+
52+
// const data = (await updateDatabase(opLikes + 1).then((r) => r.json())) as ResponseData
53+
54+
// if (!data.error) {
55+
// console.log(data.likes)
56+
// setLikes(data.likes)
57+
// } else {
58+
// setLikes(data.likes)
59+
// setError(data.error)
60+
// }
61+
// }
62+
63+
// return (
64+
// <form onSubmit={submit} className="space-y-6">
65+
// <div>
66+
// <button type="submit" className="button text-xl">
67+
// Like My Post: {opLikes}
68+
// </button>
69+
// </div>
70+
// {error && <div className="text-red-800">{error}</div>}
71+
// </form>
72+
// )
73+
// }

Diff for: react/advanced-hooks/05-form-actions/lecture/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function Pending({ children }: { children: React.ReactNode }) {
300300
// }
301301

302302
/**
303-
* Example 5: When to NOT use useActionState: rapid-fire submissions submissions
303+
* Example 5: When to NOT use useActionState: rapid-fire repeat submissions
304304
*/
305305

306306
// START --------------------> Resolve

Diff for: remix/lessons/01-routes-and-layouts/practice-final/root.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function App() {
2323
<MainLayout>
2424
<Outlet />
2525
</MainLayout>
26+
2627
<ScrollRestoration />
2728
<Scripts />
2829
<LiveReload />

Diff for: remix/lessons/01-routes-and-layouts/practice-final/routes/auth.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import { redirect, type LoaderFunctionArgs } from '@remix-run/node'
12
import { Outlet } from '@remix-run/react'
23

4+
export async function loader({ request }: LoaderFunctionArgs) {
5+
if (request.url.replace(/\/$/, '').endsWith('auth')) {
6+
return redirect('/auth/login')
7+
}
8+
9+
return null
10+
}
11+
312
export default function Page() {
413
return (
514
<div className="p-3 bg-green-300 border rounded space-y-3">

0 commit comments

Comments
 (0)