Extending the tutorial app's loading/suspense behavior to delete invoice
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 4.8k
- Forks
- 2.2k
- PR merge metrics
- No merged PRs in 30d
Description
Hi all,
I simulate a slow deleteInvoice() Server Action. No loading state/indicator applies. Perhaps, it is worth to enhance the tutorial with a best practice pattern. Or just read here about my solution. It is based on this chapter "Pending states" and could serve as a starting point for more advanced solutions.
- Slow down the
deleteInvoice()Server Action
export async function deleteInvoice(id: string) {
await new Promise((resolve) => setTimeout(resolve, 3000));
// ...
Play around and see that deleting invoices is confusing now.
- Create a separate
DeleteInvoiceButtonclient component
"use client"
import { TrashIcon, ClockIcon } from '@heroicons/react/24/outline';
import { useFormStatus } from 'react-dom';
export default function DeleteInvoiceButton() {
const { pending } = useFormStatus();
if (pending) {
return (
<button disabled className="rounded-md border p-2" aria-disabled>
<ClockIcon className="w-5 text-gray-300" />
</button>)
} else {
return (
<button className="rounded-md border p-2 hover:bg-gray-100">
<TrashIcon className="w-5" />
</button>)
}
}
- Use this component in
/app/ui/invoices/buttons.tsx
//...
import DeleteInvoiceButton from '@/app/ui/invoices/delete-invoice-button';
//...
export function DeleteInvoice({ id }: { id: string }) {
const deleteInvoiceWithId = deleteInvoice.bind(null, id);
return (
<form action={deleteInvoiceWithId}>
<DeleteInvoiceButton />
</form>
);
}
//...
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the tutorial's deleteInvoice Server Action and reviewing app/ui/invoices/buttons.tsx alongside the linked Next.js pending states guidance. Confirm the tutorial demonstrates a pending indicator while deletion is delayed, and verify the resulting example with the repository's available checks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nextjs, react, typescript
- Domain
- frontend
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100