react-hooks-game-loop

Create custom React hooks in TypeScript for game loops and animations. Use when the user asks for a React hook, requestAnimationFrame integration, game loop pattern, or delta-time handling. Handles stale-closure safety, unmount cleanup, error resilience, and configurable parameters.

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "react-hooks-game-loop" with this command: npx skills add ravenquasar/react-game-loop

React Game Loop Hooks

Production-ready custom React hooks for game loops and animations using requestAnimationFrame.

Installation

Copy the hook file into your project:

# From your skill directory
cp <skill-dir>/assets/hooks/useGameLoop.ts src/hooks/

<skill-dir> resolves to your OpenClaw skill installation directory.

Dependencies

  • React >= 16.8 (hooks support)
  • TypeScript >= 4.0 (for type safety)
  • react-dom >= 16.8 (for requestAnimationFrame types in browser environment)

This is a browser-only hook — it requires requestAnimationFrame, which is not available in Node.js or SSR environments.

API Reference

useGameLoop(options)

import { useGameLoop } from './hooks/useGameLoop';

Parameters:

OptionTypeDefaultDescription
onFrame(deltaTime: number, elapsedTime: number) => voidCalled every frame with delta (ms) and elapsed time (ms). Required.
maxDeltaTimenumber100Maximum delta clamp in ms. Prevents huge jumps after tab-switch or lag.

Returns:

FieldTypeDescription
start() => voidStart the game loop
stop() => voidStop the game loop
isRunningbooleanCurrent loop state

Patterns

Stale-closure safety

Use useRef for callbacks/state read inside async/closure contexts (e.g. requestAnimationFrame, setInterval, event listeners). Always sync ref in useEffect.

const cbRef = useRef(onFrame);
useEffect(() => { cbRef.current = onFrame; }, [onFrame]);

Cleanup on unmount

Always return cleanup from useEffect. For RAF/intervals, store handle in ref and cancel on unmount.

useEffect(() => {
  const id = requestAnimationFrame(tick);
  return () => cancelAnimationFrame(id);
}, []);

Error resilience

Wrap user callbacks in try/catch so errors don't break internal loop state.

try {
  callbackRef.current(delta, elapsed);
} catch (err) {
  console.error('hook callback error:', err);
}

Delta-time clamping

For game loops, clamp deltaTime to avoid huge jumps after tab-switch or lag spikes.

const delta = Math.min(rawDelta, maxDeltaTime);

Example

See assets/examples/GameTimer.tsx for a complete usage example showing elapsed time and FPS display.

Reference

  • assets/hooks/useGameLoop.ts — requestAnimationFrame-based game loop with start/stop/isRunning controls, deltaTime clamping, and error resilience
  • assets/examples/GameTimer.tsx — basic usage example

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

Biome

Lint and format frontend code with Biome 2.4. Covers type-aware linting, GritQL custom rules, domains, import organizer, and migration from ESLint/Prettier....

Registry SourceRecently Updated
Coding

Python Development Setup

Opinionated Python development setup with uv + ty + ruff + pytest + just. Use when creating new Python projects, setting up pyproject.toml, configuring linti...

Registry SourceRecently Updated
Coding

deep-java-review

Java项目代码review工具。分析Git变更+完整调用链路上下文,推断业务需求,进行多维度评分和分类汇总,生成完整PRD文档。包含细粒度Java代码审查清单(Null安全、异常处理、Streams、并发、equals/hashCode、资源管理、API设计、性能、MyBatis/ORM、事务边界、SQL/DD...

Registry SourceRecently Updated
Coding

ccdb

CCDB Carbon Emission Factor Search Tool. Based on the Carbonstop CCDB database, it queries carbon emission factor data via the `@carbonstop/ccdb` CLI. Suppor...

Registry SourceRecently Updated