Skip to content
Frontend
Skill

/js-tosorted-immutable

Use toSorted() instead of sort() to avoid mutating arrays. Apply when sorting arrays that are React props, state, or otherwise shared/referenced elsewhere.

From plugin
8bitcn-ui
2.1k23 skills4 hooks

Claude Code

$ npx -y skills add theorcdev/8bitcn-ui --skill js-tosorted-immutable --agent claude-code

SKILL.md

js-tosorted-immutable.SKILL.md
name: js-tosorted-immutable
description: Use toSorted() instead of sort() to avoid mutating arrays. Apply when sorting arrays that are React props, state, or otherwise shared/referenced elsewhere.

Use toSorted() Instead of sort() for Immutability

`.sort()` mutates the array in place, which can cause bugs with React Use `.toSorted()` to create a new sorted array without state and props. mutation.

**Incorrect (mutates original array):**

function UserList({ users }: { users: User[] }) {
  // Mutates the users prop array!
  const sorted = useMemo(
    () => users.sort((a, b) => a.name.localeCompare(b.name)),
    [users]
  )
  return <div>{sorted.map(renderUser)}</div>
}

**Correct (creates new array):**

function UserList({ users }: { users: User[] }) {
  // Creates new sorted array, original unchanged
  const sorted = useMemo(
    () => users.toSorted((a, b) => a.name.localeCompare(b.name)),
    [users]
  )
  return <div>{sorted.map(renderUser)}</div>
}

**Why this matters in React:**

1. Props/state mutations break React's immutability model - React expects props and state to be treated as read-only 2. Causes stale closure bugs - Mutating arrays inside closures (callbacks, effects) can lead to unexpected behavior

**Browser support (fallback for older browsers):**

`.toSorted()` is available in all modern browsers (Chrome 110+, Safari 16+, Firefox 115+, Node.js 20+). For older environments, use spread operator:

// Fallback for older browsers
const sorted = [...items].sort((a, b) => a.value - b.value)

**Other immutable array methods:**

  • `.toSorted()` - immutable sort
  • `.toReversed()` - immutable reverse
  • `.toSpliced()` - immutable splice
  • `.with()` - immutable element replacement
Read more
@theorcdev
Ships with8bitcn-ui

Accessible retro components that you can copy and paste into your apps.Free. Open Source. Built on shadcn/ui. Documentation · Components · Blocks · Themes · Discord

Stars on GitHub

Momentum

stars in total
2.1k
Star history of theorcdev/8bitcn-ui on GitHub, a chart by star-history.com

Other skills on 8bitcn-ui.

8bit-docs-patterns

8bit-docs-patterns

Create documentation with gaming-specific examples, retro styling, and 8-bit terminology. Apply when documenting gaming blocks,…

@theorcdev@theorcdevInstall Skill
bundle-barrel-imports

bundle-barrel-imports

Import directly from source files instead of barrel files. Apply when using libraries like lucide-react, @mui/material, or…

@theorcdev@theorcdevInstall Skill

More skills from other plugins.

next-bundle-optimizer
Auto-invoked

next-bundle-optimizer

Audit and reduce Next.js browser initial-load work. Use for slow route startup, oversized client bundles, duplicate browser…

nextjs10d143k
@vercel@vercelInstall Skill
migrate-radix-to-base

migrate-radix-to-base

Migrates React projects and components from Radix UI to Base UI. Use when asked to migrate from radix, move to base-ui, convert…

ui2w127k
@shadcn-ui@shadcn-uiInstall Skill
chrome-release-cls

chrome-release-cls

Given a Chrome Releases blog post URL (chromereleases.googleblog.com), extract every CVE/bug and find the underlying Gerrit CL…

electron10d124k
@electron@electronInstall Skill
taste-skill
Auto-invoked

taste-skill

tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One…

Questions about js-tosorted-immutable.

Which coding agents can use js-tosorted-immutable?
js-tosorted-immutable installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.