11import { useOptimistic , useState , useTransition } from 'react'
22import { 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
78export 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) {
0 commit comments