R3f best practices

From ngophatneknha/ai-engineering-hub · View interactive version

R3f best practices is an AI agent skill from the GitHub repository ngophatneknha/ai-engineering-hub. React Three Fiber (R3F) and Poimandres ecosystem best practices. Use when writing, reviewing, or optimizing R3F code. Triggers on tasks involving @react-three/fiber, @react-three/drei, zustand, @react-three/postprocessing, @react-three/rapier, or leva.

How to use this skill

Skill file

React Three Fiber Best Practices

Comprehensive guide for React Three Fiber and the Poimandres ecosystem. Contains 70+ rules across 12 categories, prioritized by impact.

Sources & Credits

Additional tips from 100 Three.js Tips by Utsubo

When to Apply

Reference these guidelines when:

Ecosystem Coverage

Rule Categories by Priority

| Priority | Category | Impact | Prefix |

|----------|----------|--------|--------|

| 1 | Performance & Re-renders | CRITICAL | perf- |

| 2 | useFrame & Animation | CRITICAL | frame- |

| 3 | Component Patterns | HIGH | component- |

| 4 | Canvas & Setup | HIGH | canvas- |

| 5 | Drei Helpers | MEDIUM-HIGH | drei- |

| 6 | Loading & Suspense | MEDIUM-HIGH | loading- |

| 7 | State Management | MEDIUM | state- |

| 8 | Events & Interaction | MEDIUM | events- |

| 9 | Post-processing | MEDIUM | postpro- |

| 10 | Physics (Rapier) | LOW-MEDIUM | physics- |

| 11 | Leva (Debug GUI) | LOW | leva- |

Quick Reference

1. Performance & Re-renders (CRITICAL)

2. useFrame & Animation (CRITICAL)

3. Component Patterns (HIGH)

4. Canvas & Setup (HIGH)

5. Drei Helpers (MEDIUM-HIGH)

6. Loading & Suspense (MEDIUM-HIGH)

7. State Management (MEDIUM)

8. Events & Interaction (MEDIUM)

9. Post-processing (MEDIUM)

10. Physics Rapier (LOW-MEDIUM)

11. Leva (LOW)

How to Use

Read individual rule files for detailed explanations and code examples. Only these rule files ship with this copy; for every other rule, use its one-line summary above:

rules/perf-never-set-state-in-useframe.md
rules/perf-zustand-selectors.md
rules/perf-visibility-toggle.md
rules/perf-r3f-perf.md
rules/frame-delta-time.md
rules/drei-use-gltf.md
rules/loading-suspense.md

Critical Patterns

NEVER setState in useFrame

// BAD - 60 re-renders per second!
function BadComponent() {
  const [position, setPosition] = useState(0);
  useFrame(() => {
    setPosition(p => p + 0.01); // NEVER DO THIS
  });
  return <mesh position-x={position} />;
}

// GOOD - Mutate refs directly
function GoodComponent() {
  const meshRef = useRef();
  useFrame(() => {
    meshRef.current.position.x += 0.01;
  });
  return <mesh ref={meshRef} />;
}

Zustand Selectors

// BAD - Re-renders on ANY store change
const store = useGameStore();

// GOOD - Only re-renders when playerX changes
const playerX = useGameStore(state => state.playerX);

// BETTER - No re-renders, direct mutation
useFrame(() => {
  const { value } = useStore.getState();
  ref.current.position.x = value;
});

Drei useGLTF

import { useGLTF } from '@react-three/drei';

function Model() {
  const { scene } = useGLTF('/model.glb');
  return <primitive object={scene} />;
}

// Preload for instant loading
useGLTF.preload('/model.glb');

Suspense Loading

function App() {
  return (
    <Canvas>
      <Suspense fallback={<Loader />}>
        <Model />
      </Suspense>
    </Canvas>
  );
}

r3f-perf Monitoring

import { Perf } from 'r3f-perf';

function App() {
  return (
    <Canvas>
      <Perf position="top-left" />
      <Scene />
    </Canvas>
  );
}

Toggle Visibility (Not Remounting)

// BAD: Remounting destroys and recreates
{showModel && <Model />}

// GOOD: Toggle visibility, keeps instance alive
<Model visible={showModel} />
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.