Last active
May 29, 2022 04:55
-
-
Save itsMapleLeaf/8d5e8807be5f63ffb79a2645f2c0ded1 to your computer and use it in GitHub Desktop.
suspense for mutation?
This file contains hidden or 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
import { useState, useTransition } from "react" | |
export function NoteScreen({ noteId }) { | |
const [resource, setResource] = useState(() => | |
createResource(getNote(noteId)), | |
) | |
const note = resource.read() | |
const [text, setText] = useState(note.text) | |
const [pending, startTransition] = useTransition() | |
const handleSubmit = () => { | |
startTransition(() => { | |
setResource(createResource(updateNote(noteId, { text }))) | |
}) | |
} | |
return ( | |
<form onSubmit={preventDefault(handleSubmit)}> | |
<textarea value={text} onChange={(e) => setText(e.target.value)} /> | |
{pending ? <p>Saving...</p> : <button type="submit">Save</button>} | |
</form> | |
) | |
} |
This file contains hidden or 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
import { useState } from "react" | |
import { useMutation } from "react-query" | |
import { signIn } from "./auth" | |
import { preventDefault } from "./form-helpers" | |
export function SignIn() { | |
const [username, setUsername] = useState("") | |
const [password, setPassword] = useState("") | |
const mutation = useMutation(() => signIn(username, password), { | |
onSuccess: () => history.push("/dashboard"), | |
}) | |
return ( | |
<form onSubmit={preventDefault(mutation.mutate)}> | |
<input | |
type="text" | |
value={username} | |
onChange={(e) => setUsername(e.target.value)} | |
/> | |
<input | |
type="password" | |
value={password} | |
onChange={(e) => setPassword(e.target.value)} | |
/> | |
<button type="submit">Sign in</button> | |
{mutation.isLoading && <LoadingSpinner />} | |
{mutation.error && <ErrorMessage error={mutation.error} />} | |
</form> | |
) | |
} |
This file contains hidden or 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
import { Suspense, useState, useTransition } from "react" | |
import { ErrorBoundary } from "react-error-boundary" | |
import { createResource } from "some-magic-suspense-library" | |
import { signIn } from "./auth" | |
import { preventDefault } from "./form-helpers" | |
export function SignInScreen() { | |
return ( | |
<ErrorBoundary renderFallback={({ error }) => <SignInForm error={error} />}> | |
<Suspense> | |
<SignInForm /> | |
</Suspense> | |
</ErrorBoundary> | |
) | |
} | |
function SignInForm({ error }) { | |
const [username, setUsername] = useState("") | |
const [password, setPassword] = useState("") | |
const [pending, startTransition] = useTransition() | |
const [resource, setResource] = useState() | |
resource?.read() | |
const handleSubmit = () => { | |
startTransition(() => { | |
const promise = signIn(username, password) | |
setResource(createResource(promise)) | |
promise.then(() => history.push("/dashboard")) | |
}) | |
} | |
return ( | |
<form onSubmit={preventDefault(() => startTransition(handleSubmit))}> | |
<input | |
type="text" | |
value={username} | |
onChange={(e) => setUsername(e.target.value)} | |
/> | |
<input | |
type="password" | |
value={password} | |
onChange={(e) => setPassword(e.target.value)} | |
/> | |
<button type="submit">Sign in</button> | |
{pending && <LoadingSpinner />} | |
{error && <ErrorMessage error={error} />} | |
</form> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment