-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update React hooks for optimistic form submission
- Loading branch information
1 parent
730986f
commit 3ab9db2
Showing
3 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useActionState, useOptimistic } from "react"; | ||
|
||
type ReturnResponse = { | ||
ok: boolean; | ||
message?: string; | ||
}; | ||
|
||
type FormYourNameProps = { | ||
name: string; | ||
}; | ||
|
||
async function updateName(props: FormYourNameProps): Promise<ReturnResponse> { | ||
console.log(props); | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
if (props.name.length < 3) { | ||
return { | ||
ok: false, | ||
message: "Name is too short", | ||
}; | ||
} | ||
return { | ||
ok: true, | ||
}; | ||
} | ||
|
||
const parseFormData = (formData: FormData) => { | ||
const name = formData.get("name") as string; | ||
return { | ||
name, | ||
}; | ||
}; | ||
|
||
export const UseOptimisticActionStateForm = () => { | ||
const [name, setName] = useOptimistic("name"); | ||
const [response, submitAction, isPending] = useActionState< | ||
ReturnResponse | null, | ||
FormData | ||
>( | ||
async (_, formData) => { | ||
const props = parseFormData(formData); | ||
setName(props.name); | ||
const error = await updateName(props); | ||
if (!error.ok) { | ||
return error; | ||
} | ||
// redirect to /finished without 3rd argument | ||
// redirect("/finished", RedirectType.push); | ||
return null; | ||
}, | ||
null, | ||
// redirect to /finished | ||
"/finished" | ||
); | ||
|
||
return ( | ||
<div> | ||
<form action={submitAction}> | ||
<p>Current Name: {name}</p> | ||
<label> | ||
New Name: | ||
<input type="text" name="name" /> | ||
</label> | ||
<button type="submit">Submit</button> | ||
{isPending && <p>Submitting...</p>} | ||
{response && <p>{response.message}</p>} | ||
</form> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useOptimistic, useRef, useState } from "react"; | ||
|
||
const deliverTitle = async (title: string) => { | ||
await new Promise((res) => setTimeout(res, 1000)); | ||
return title; | ||
}; | ||
export const UseOptimisticStateForm = () => { | ||
const formRef = useRef<HTMLFormElement | null>(null); | ||
|
||
const [title, setTitle] = useState(""); | ||
|
||
const [optimisticTitle, addOptimisticTitle] = useOptimistic<string, string>( | ||
title, | ||
(_currentState, optimisticValue) => optimisticValue | ||
); | ||
|
||
const sendTitle = async (formData: FormData) => { | ||
const sentTitle = await deliverTitle(formData.get("title") as string); | ||
setTitle(() => sentTitle); | ||
}; | ||
|
||
const formAction = async (formData: FormData) => { | ||
addOptimisticTitle(formData.get("title") as string); | ||
formRef.current?.reset(); | ||
await sendTitle(formData); | ||
}; | ||
|
||
return ( | ||
<> | ||
<form ref={formRef} action={formAction}> | ||
optimatic : {optimisticTitle} | ||
<div /> | ||
label : {title} | ||
<div /> | ||
<input name="title" type="text" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</> | ||
); | ||
}; |