Skip to content

Commit 362dc45

Browse files
committed
starting point
1 parent 3e26717 commit 362dc45

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

react/advanced-hooks/05-optimistic-ui/lecture/App.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useOptimistic, useState, useTransition } from 'react'
22
import { type ResponseData, updateDatabase } from './helpers/mockServer'
33

4-
// Remember that useOptimistic will not work with <form onSubmit />, and requires
5-
// <form action />
4+
// Big takeaways:
5+
// 1. Homegrown optimistic state works with onSubmit, not with actions
6+
// 2. useOptimistic works with actions, not on submit
67

78
export function App() {
89
const [error, setError] = useState('')
@@ -13,6 +14,7 @@ export function App() {
1314

1415
const data = (await updateDatabase(likes + 1).then((r) => r.json())) as ResponseData
1516
setLikes(data.likes)
17+
1618
console.log(data.likes)
1719

1820
if (data.error) {
@@ -32,7 +34,7 @@ export function App() {
3234
)
3335
}
3436

35-
// // Step Two: home-made optimistic UI with transitions
37+
// // Step Two: home-made optimistic UI
3638
// export function App() {
3739
// const [error, setError] = useState('')
3840
// const [likes, setLikes] = useState(0)
@@ -43,12 +45,11 @@ export function App() {
4345
// async function submit(e: React.FormEvent) {
4446
// e.preventDefault()
4547

46-
// startTransition(() => {
47-
// setOpLikes(opLikes + 1)
48-
// })
48+
// setOpLikes(opLikes + 1)
4949

5050
// const data = (await updateDatabase(opLikes + 1).then((r) => r.json())) as ResponseData
5151
// setLikes(data.likes)
52+
5253
// console.log(data.likes)
5354

5455
// if (data.error) {

react/advanced-hooks/05-optimistic-ui/lecture/NOTES.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
Start with a basic form submission that asynchronously updates likes:
44

55
1. Add our own implementation of `optimisticLikes` with `useState`. Notice how
6-
`likes` becomes useless / unused. We set it but nothing uses it. Also re-factor
7-
to use transitions. See commented solution in App.tsx
6+
`likes` becomes useless / unused. We set it but nothing uses it.
87

98
2. Refactor `onSubmit={submit}` to `action={action}` and now the homegrown optimistic likes
109
don't work.

0 commit comments

Comments
 (0)