All skills
sergiodxa avatar

/frontend-react-router-best-practices

@9da9532

React Router performance and architecture patterns. Use when writing loaders, actions, forms, routes, or working with React Router data fetching. Triggers on tasks involving React Router routes, data loading, form handling, or route organization.

Use this Skill: https://skilld.dev/gh/sergiodxa/agent-skills/frontend-react-router-best-practices

This session only. Nothing lands on disk.

rulesform-pending-state.md

≈657 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Form Pending States

Show loading states with useNavigation or fetcher.state. Use useSpinDelay to avoid flicker.

Why

  • Users need feedback that action is processing
  • Prevents double-submission
  • Quick actions shouldn't flash loading state (flicker)

Basic Fetcher Pending State

function SubmitButton() {
  let fetcher = useFetcher();
  let isPending = fetcher.state !== "idle";

  return (
    <Button type="submit" isDisabled={isPending}>
      {isPending ? "Submitting..." : "Submit"}
    </Button>
  );
}

With Spinner and useSpinDelay

Avoid flicker for fast operations:

import { useSpinDelay } from "spin-delay";

function SubmitButton() {
  let fetcher = useFetcher();

  // Only show spinner if pending for >50ms
  let isPending = useSpinDelay(fetcher.state !== "idle", { delay: 50 });

  return (
    <Button type="submit" isDisabled={isPending} className="relative">
      {isPending && (
        <div className="absolute inset-0 center">
          <Spinner />
        </div>
      )}
      <span className={isPending ? "invisible" : ""}>Submit</span>
    </Button>
  );
}

useNavigation for Form Component

import { Form, useNavigation } from "react-router";

function CreateForm() {
  let navigation = useNavigation();

  // Check if THIS form is submitting
  let isSubmitting =
    navigation.state === "submitting" && navigation.formAction === "/items/new";

  return (
    <Form method="post" action="/items/new">
      <input name="title" disabled={isSubmitting} />
      <Button type="submit" isDisabled={isSubmitting}>
        {isSubmitting ? "Creating..." : "Create"}
      </Button>
    </Form>
  );
}

Disable Form During Submission

function EditForm() {
  let fetcher = useFetcher();
  let isPending = fetcher.state !== "idle";

  return (
    <fetcher.Form method="post">
      <fieldset disabled={isPending}>
        <input name="name" />
        <input name="email" />
        <Button type="submit">{isPending ? "Saving..." : "Save"}</Button>
      </fieldset>
    </fetcher.Form>
  );
}

Prevent Modal Close During Submission

function EditModal({ isOpen, onClose }) {
  let fetcher = useFetcher();
  let isPending = fetcher.state !== "idle";

  return (
    <Modal
      isOpen={isOpen}
      onOpenChange={(open) => {
        // Don't allow close while submitting
        if (!open && isPending) return;
        if (!open) onClose();
      }}
    >
      <fetcher.Form method="post">{/* Form content */}</fetcher.Form>
    </Modal>
  );
}

Source: SKILL.md on GitHub

2 warnings16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive set of developer guidelines and best practices for building React Router applications. It emphasizes performance, type safety, and security measures like input validation (Zod), safe redirects, and CSRF protection (Sec-Fetch). No malicious code, obfuscation, or data exfiltration patterns were detected.

  • Socket16d

    1 alert: gptSecurity

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    56/56 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 9da9532. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 8 months ago

README badge

README badge for sergiodxa/agent-skills/frontend-react-router-best-practices