- docs/agent/external: Vercel react-best-practices and composition-patterns (MIT), Addy Osmani accessibility (MIT), Anthropic frontend-design (Apache-2.0), official Payload skill at v3.92.1 (MIT) — text only, each with its licence, SOURCE.md (repo, commit) and an override note in front of the SKILL.md (ipal-kit first, Payload fetching over swr, DESIGN.md tokens, Payload 3 not 4); manifest.json with sha256 per file - scripts/vendor-external-skills.mjs (maintainer, not shipped): fetches by full commit SHA only, rejects non-text or executable files, regenerates the manifest; --check verifies it - ipal-skills --external [names]: copies ext-* after checking every file against the manifest, downloads nothing, never touches ext-* folders without ipal-kit's SOURCE.md, removes only retired ones it installed; an unknown name stops the run before anything is copied - AGENT.md §14, ipal-setup stage 2 (optional step), README: usage, overrides, how to update the pins - test/externalSkills.test.ts: pins are SHAs, no branch fetch, checksums, text only, licences, override note, installer leaves other folders alone Co-Authored-By: Claude Opus 5.5 <[email protected]>
76 lines
2.0 KiB
Markdown
76 lines
2.0 KiB
Markdown
---
|
|
title: Use useTransition Over Manual Loading States
|
|
impact: LOW
|
|
impactDescription: reduces re-renders and improves code clarity
|
|
tags: rendering, transitions, useTransition, loading, state
|
|
---
|
|
|
|
## Use useTransition Over Manual Loading States
|
|
|
|
Use `useTransition` instead of manual `useState` for loading states. This provides built-in `isPending` state and automatically manages transitions.
|
|
|
|
**Incorrect (manual loading state):**
|
|
|
|
```tsx
|
|
function SearchResults() {
|
|
const [query, setQuery] = useState('')
|
|
const [results, setResults] = useState([])
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const handleSearch = async (value: string) => {
|
|
setIsLoading(true)
|
|
setQuery(value)
|
|
const data = await fetchResults(value)
|
|
setResults(data)
|
|
setIsLoading(false)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<input onChange={(e) => handleSearch(e.target.value)} />
|
|
{isLoading && <Spinner />}
|
|
<ResultsList results={results} />
|
|
</>
|
|
)
|
|
}
|
|
```
|
|
|
|
**Correct (useTransition with built-in pending state):**
|
|
|
|
```tsx
|
|
import { useTransition, useState } from 'react'
|
|
|
|
function SearchResults() {
|
|
const [query, setQuery] = useState('')
|
|
const [results, setResults] = useState([])
|
|
const [isPending, startTransition] = useTransition()
|
|
|
|
const handleSearch = (value: string) => {
|
|
setQuery(value) // Update input immediately
|
|
|
|
startTransition(async () => {
|
|
// Fetch and update results
|
|
const data = await fetchResults(value)
|
|
setResults(data)
|
|
})
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<input onChange={(e) => handleSearch(e.target.value)} />
|
|
{isPending && <Spinner />}
|
|
<ResultsList results={results} />
|
|
</>
|
|
)
|
|
}
|
|
```
|
|
|
|
**Benefits:**
|
|
|
|
- **Automatic pending state**: No need to manually manage `setIsLoading(true/false)`
|
|
- **Error resilience**: Pending state correctly resets even if the transition throws
|
|
- **Better responsiveness**: Keeps the UI responsive during updates
|
|
- **Interrupt handling**: New transitions automatically cancel pending ones
|
|
|
|
Reference: [useTransition](https://react.dev/reference/react/useTransition)
|