zebbern/claude-code-guide / skills/r3f-best-practices
r3f-best-practices
VerifiedReact 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.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 7.9 KB |
| rules/_sections.md | markdown | 1.9 KB |
| rules/drei-use-gltf.md | markdown | 3.0 KB |
| rules/frame-delta-time.md | markdown | 2.7 KB |
| rules/loading-suspense.md | markdown | 3.6 KB |
| rules/perf-never-set-state-in-useframe.md | markdown | 2.6 KB |
| rules/perf-r3f-perf.md | markdown | 1.9 KB |
| rules/perf-visibility-toggle.md | markdown | 1.9 KB |
| rules/perf-zustand-selectors.md | markdown | 3.0 KB |
rules/perf-visibility-toggle.md
View on GitHub# Toggle Visibility Instead of Remounting
> Source: [100 Three.js Tips - Utsubo](https://www.utsubo.com/blog/threejs-best-practices-100-tips)
Toggle the `visible` prop instead of conditionally mounting/unmounting components.
## Why It Matters
Remounting a component:
1. Destroys the Three.js object
2. Triggers disposal (if configured)
3. Creates new geometry/material
4. Uploads new data to GPU
5. Recompiles shaders
Toggling visibility:
1. Sets `object.visible = false`
2. That's it - object stays in memory
## BAD: Conditional Mounting
```jsx
function Scene({ showModel }) {
return (
<>
{showModel && <ExpensiveModel />}
</>
);
}
```
Every toggle destroys and recreates the entire model.
## GOOD: Visibility Toggle
```jsx
function Scene({ showModel }) {
return (
<ExpensiveModel visible={showModel} />
);
}
```
Model stays in memory, just skipped during render.
## When to Use Each
### Use Visibility Toggle When:
- Frequent show/hide (e.g., UI state)
- Object is expensive to create
- Object is needed again soon
- Object count is manageable
### Use Conditional Mounting When:
- Object is rarely shown
- Memory is constrained
- Object is cheap to create
- Large number of potential objects
## With Refs
```jsx
function ToggleableModel() {
const meshRef = useRef();
const [visible, setVisible] = useState(true);
// Direct mutation for animations
useFrame(() => {
if (meshRef.current) {
meshRef.current.visible = shouldBeVisible;
}
});
return <mesh ref={meshRef} visible={visible} />;
}
```
## Visibility vs Layers
For complex visibility logic, consider using Three.js layers:
```jsx
function SelectiveRendering() {
const meshRef = useRef();
useEffect(() => {
// Set to layer 1 (not rendered by default camera)
meshRef.current.layers.set(1);
}, []);
return <mesh ref={meshRef} />;
}
```
Layers allow camera-selective rendering without changing visibility.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |