Toggle Visibility Instead of Remounting
Source: 100 Three.js Tips - Utsubo
Toggle the visible prop instead of conditionally mounting/unmounting components.
Why It Matters
Remounting a component:
- Destroys the Three.js object
- Triggers disposal (if configured)
- Creates new geometry/material
- Uploads new data to GPU
- Recompiles shaders
Toggling visibility:
- Sets
object.visible = false - 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 (
);
}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.