From the archive · · 4 min read
React Three Fiber or vanilla Three.js?
Rendering approaches, scene ownership, and React integration for interactive web graphics.
An earlier article, retained in the writing archive. Tool names and setup commands reflect the original publication.
The web has come a long way—from static pages to immersive, interactive experiences. At the center of this evolution is Three.js, a powerful JavaScript library for crafting 3D graphics. But as developers, we’re always searching for tools that make our work cleaner and our code easier to maintain. Enter React Three Fiber, a React-based approach to working with Three.js that promises a more intuitive workflow.
If you’re deciding which one to use for your project, let’s break it down.
What is Three.js?
Three.js is a lightweight, versatile library for rendering 3D content in the browser. It’s essentially your gateway to WebGL without diving into its complexities. With Three.js, you can build:
- Complex 3D scenes
- Real-time animations
- Fully interactive environments
It’s powerful, but there’s a catch—it demands a lot of manual work. You’re in charge of:
- Managing the scene graph
- Cleaning up resources
- Writing boilerplate code for basic setups
What is React Three Fiber?
React Three Fiber (R3F) takes a declarative approach, blending React’s component model with Three.js’s capabilities. It lets you describe 3D scenes as React components, eliminating much of the boilerplate and improving readability.
import { Canvas } from "@react-three/fiber";
function Box() {
return (
<mesh>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="orange" />
</mesh>
);
}
export default function App() {
return (
<Canvas>
<Box />
</Canvas>
);
}
By abstracting the render loop and resource management, R3F allows you to focus on your scene.
Key Differences
1. Programming Paradigm
- Three.js: Imperative—you explicitly instruct the library step by step.
- React Three Fiber: Declarative—you describe the desired outcome, and React handles the details.
2. Learning Curve
- Three.js: Steeper, especially if you’re unfamiliar with WebGL concepts.
- React Three Fiber: Easier if you’re already comfortable with React.
3. Boilerplate
- Three.js: High. Setting up cameras, lights, and render loops takes effort.
- React Three Fiber: Low. Much of the setup is handled for you.
4. Performance
- Three.js: Direct control means better fine-tuning for high-performance needs.
- React Three Fiber: Slightly more overhead due to React, but still efficient for most cases.
5. Ecosystem
- Three.js: Established, with countless plugins and community resources.
- React Three Fiber: Growing, with tools like
drei(helpers) andrapier(physics).
When to Use Three.js
- Full Control – ideal for projects needing granular customization and optimization.
- Non-React Projects – if React isn’t part of your stack, stick with vanilla Three.js.
- Learning WebGL – developers diving into WebGL concepts will benefit from Three.js’s closer-to-the-metal approach.
- Game Development – for complex games with advanced shaders and physics, Three.js gives you the flexibility you need.
When to Use React Three Fiber
- React-Based Projects – if your app is already in React, R3F integrates seamlessly.
- Rapid Prototyping – the declarative style speeds up iteration and development.
- Maintainable Code – React’s component structure keeps your 3D scenes modular and reusable.
- UI and 3D Integration – combining 3D content with standard UI elements is smoother with R3F.
Rendering a cube
Three.js Version
import * as THREE from "three";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000,
);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
React Three Fiber Version
import { Canvas } from "@react-three/fiber";
function Box() {
return (
<mesh rotation={[0.4, 0.2, 0]}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="orange" />
</mesh>
);
}
export default function App() {
return (
<Canvas camera={{ position: [0, 0, 5] }}>
<ambientLight intensity={0.5} />
<directionalLight position={[5, 5, 5]} />
<Box />
</Canvas>
);
}
Final Thoughts
Use Three.js when you need absolute control or performance tuning, and pick React Three Fiber when you want the power of Three.js wrapped in React’s ergonomics. Either way, you’re building the web’s future. Choose the tool that matches your project’s constraints—and your team’s strengths.