agents/skills/react-patterns/SKILL.md
React patterns for callbacks, event handlers, and module-level constants. Use when writing React components, implementing event handlers, or defining constants.
npx skillsauth add firtoz/cf-multiworker-starter-kit react-patternsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Use useCallback for callbacks and event handlers to ensure stable references and optimal performance.
const handleClick = useCallback(() => {
// Handler logic
}, [dependencies]);
const handleChange = useCallback((value: string) => {
setState(value);
}, [setState]);
Use useEffectEvent for callbacks inside effects when you need to reference the latest props/state without adding them to dependencies:
import { useEffectEvent } from "react";
function Component({ onUpdate }) {
const [value, setValue] = useState("");
// Latest props/state without re-triggering effect
const handleUpdate = useEffectEvent(() => {
onUpdate(value);
});
useEffect(() => {
const interval = setInterval(() => {
handleUpdate(); // Always calls with latest value
}, 1000);
return () => clearInterval(interval);
}, []); // No dependencies needed
}
Define true constants at module level, not inside components. Constants that never change should be defined once, not recreated on every render.
// ✅ Good - Module level constant
const DEFAULT_CONFIG = {
timeout: 5000,
retries: 3,
};
const DEFAULT_HERO_JSON: JSONContent = {
type: "doc",
content: [{ type: "heading", attrs: { level: 1 }, content: [{ type: "text", text: "Title" }] }],
};
export default function MyComponent() {
const [config, setConfig] = useState(DEFAULT_CONFIG);
// ...
}
// ❌ Bad - Constant recreated on every render
export default function MyComponent() {
const defaultConfig = {
timeout: 5000,
retries: 3,
};
const [config, setConfig] = useState(defaultConfig);
// ...
}
Benefits of module-level constants:
useMemodevelopment
Repo-root commands, typegen and typecheck cadence, lint, deploy, adding packages with bun, and Alchemy app layout. Use at the start of a task, before PR, or when choosing turbo/typegen commands.
development
Fork and template gotchas (env import, routes, typegen, forms, D1, Turbo, HMR, new DO packages). Use when working on apps/web or durable-objects, or when behavior diverges from this stack’s conventions.
testing
Turborepo task configuration patterns for monorepo management. Use when configuring turbo.json tasks, setting up task dependencies, managing cache inputs/outputs, or working with cross-package dependencies in the monorepo.
development
React Router v7 routing patterns and environment variable configuration. Use whenever you touch React Router–related code (routes, links, params, loaders, actions, route config, or env in route context).