Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 39 ยท Performance and Frameworks

React Three Fiber, TresJS and Other Frameworks

Use Three.js with React Three Fiber, drei, Threlte and TresJS: how declarative scenes work, hooks like useFrame, and a runnable mini-reconciler that shows the idea behind them.

You now know Three.js in its imperative form: create objects, add them to the scene, mutate them in a loop. That is the right way to learn, and the right way for games, engines and heavy custom rendering. But if your app is mostly a normal website (a product page, a dashboard with a 3D chart, a portfolio) and you already build it with React, Vue or Svelte, hand-managing a scene next to a component tree gets messy: who creates the mesh, who disposes it, how does state reach it?

Frameworks that wrap Three.js solve this by letting you describe the scene as components and letting the framework keep the Three.js objects in sync with your state. This lesson explains the main options, the ideas behind them, and shows those ideas in a small runnable example, so you understand what the magic does.

The landscape

Library Framework Idea
React Three Fiber (R3F) React A React renderer for Three.js: JSX tags are Three.js classes
drei React (R3F) A big collection of helpers: controls, loaders, text, environment, effects
@react-three/rapier, cannon R3F Physics components
@react-three/postprocessing R3F The postprocessing effect library as components
TresJS Vue Vue components for Three.js (<TresMesh>) with a Vue-style ecosystem
Threlte Svelte Svelte components and a very small runtime overhead
A-Frame HTML Entity-component scenes written as custom HTML elements, popular for WebXR
three.js + your own code any Fully imperative, no wrapper

They all produce a normal Three.js scene underneath. You can still call new THREE.Mesh() yourself, use any addon, and read renderer.info. The wrapper manages creation, updates and disposal for you.

React Three Fiber in a nutshell

R3F is not a wrapper that re-implements Three.js; it is a custom React renderer, like react-dom but for Three.js objects instead of DOM nodes. A JSX tag whose name matches a Three.js class (in lowerCamelCase) becomes that class, and its props are applied to the instance.

JSX
import { Canvas, useFrame } from '@react-three/fiber';
import { OrbitControls, Environment } from '@react-three/drei';
import { useRef, useState } from 'react';

function Cube() {
  const ref = useRef();
  const [hovered, setHovered] = useState(false);

  useFrame((state, delta) => { ref.current.rotation.y += delta; });   // runs every frame

  return (
    <mesh ref={ref} onPointerOver={() => setHovered(true)} onPointerOut={() => setHovered(false)}>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  );
}

export default function App() {
  return (
    <Canvas camera={{ position: [0, 1.5, 4] }} shadows>
      <ambientLight intensity={0.6} />
      <directionalLight position={[3, 4, 2]} castShadow />
      <Cube />
      <OrbitControls />
      <Environment preset="city" />
    </Canvas>
  );
}

This is plain React:

  • <Canvas> creates the renderer, scene, camera, resize handling and render loop.
  • <mesh>, <boxGeometry args={[1,1,1]}> and <meshStandardMaterial> correspond to new THREE.Mesh(), new THREE.BoxGeometry(1, 1, 1) and new THREE.MeshStandardMaterial(). args are the constructor arguments, and nested children are attached as geometry and material automatically.
  • Props such as position, color and intensity set the property of the same name; array props set vectors (position={[1, 2, 3]}), and dashes reach nested properties (position-x={2}, rotation-y={Math.PI}).
  • Events (onClick, onPointerOver) are raycasting done for you.
  • useFrame runs code in the render loop; useThree gives you the renderer, camera and scene; useLoader and drei's useGLTF load assets with suspense and caching.
  • When a component unmounts, R3F disposes its geometries and materials.

Two performance rules matter when you move into React: never call setState in useFrame (re-rendering 60 times a second defeats the purpose; mutate the ref instead), and keep <Canvas frameloop="demand"> in mind for static scenes, which renders only when invalidated (the on-demand pattern from the performance lesson).

What the wrapper actually does

The key insight is simple. The framework keeps a tree of descriptions (JSX elements, Vue templates, Svelte markup) and a tree of real Three.js objects. When state changes it compares the new description with the old one and, instead of rebuilding, patches only the properties that changed. The expensive objects (geometries, materials, meshes) are created once.

How a declarative scene stays in sync

Describe Components say what the scene is
Diff Compare with the last description
Patch Set only the changed props
Dispose Remove and free what disappeared

The same loop drives react-dom, R3F, TresJS and Threlte, with different front ends.

1 / 2

R3F cannot run inside this lesson's sandbox (it needs a bundler-style React setup), so the examples below recreate its core mechanism in about forty lines of plain JavaScript. The code is not production quality, but it is real and runs: it turns descriptions into Three.js objects, then patches them.

A mini declarative renderer

First, the translation from a tag name to a Three.js class, and from props to property assignments:

JavaScript
const toClass = (tag) => tag[0].toUpperCase() + tag.slice(1);       // 'boxGeometry' -> 'BoxGeometry'
function create(node) {
  const Cls = THREE[toClass(node.type)];
  const obj = new Cls(...(node.props.args ?? []));
  apply(obj, {}, node.props);
  return obj;
}
function apply(obj, oldProps, newProps) {
  for (const [key, value] of Object.entries(newProps)) {
    if (key === 'args' || key === 'children' || oldProps[key] === value) continue;   // only changed props
    const target = obj[key];
    if (target?.set && Array.isArray(value)) target.set(...value);    // position: [x, y, z]
    else if (target?.set) target.set(value);                           // color: 'tomato'
    else obj[key] = value;                                             // intensity: 2
  }
}
Creating Three.js objects from descriptions

Without writing a single new THREE.Mesh(...), you get a scene. Everything an R3F component returns goes through this kind of translation.

Patching instead of rebuilding

Now the interesting part: state. When the application state changes, we describe the scene again and call patch(oldNode, newNode, obj), which only touches differing props. In the example below the colors and positions change every second, and a counter shows that no new Three.js objects were created after the first build.

Diff and patch: state drives the scene

Six state updates created zero new Three.js objects; only the properties that differed were assigned. That is exactly why R3F feels like React but runs like Three.js: React does the bookkeeping, Three.js objects are long lived.

Choosing: plain Three.js or a framework?

  • Use plain Three.js for games, editors, heavy custom shaders or render pipelines, and when you want the smallest dependency set or full control of the loop.
  • Use R3F, TresJS or Threlte when 3D is part of a component-based site, you want app state to drive the scene, and you value declarative composition, hot reload and the ecosystem (drei's controls, text, loaders, physics wrappers).
  • Mixing is fine: R3F lets you drop to raw Three.js via refs (ref.current is the real THREE.Mesh) and extend({ MyCustomClass }) registers your own classes as JSX tags.

A few cautions when you pick a framework: pin versions together (R3F, drei and Three.js are released separately and each R3F release targets a React version); learn the underlying Three.js API first, because errors and docs are all in Three.js terms; and keep the render loop in mind, since a re-render of a React component is much more expensive than changing a ref's property.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is React Three Fiber?

    R3F is a custom React reconciler: <mesh> becomes new THREE.Mesh, and props are applied to it. Three.js itself still does the rendering.
  2. 2. What does <boxGeometry args={[1, 2, 3]} /> correspond to?

    args is the list of constructor arguments of the Three.js class named by the tag.
  3. 3. Why should you avoid calling setState inside useFrame?

    React re-renders are expensive compared to setting a property on a Three.js object, so per-frame motion is done on refs.
  4. 4. In the mini renderer, why were no new Three.js objects created after state changes?

    Diffing the old and new description and assigning only differing props is the core idea of React-style renderers.

Summary

  • Frameworks such as React Three Fiber, TresJS, Threlte and A-Frame let you describe a scene declaratively; Three.js still renders it.
  • R3F maps lowerCamelCase tags to Three.js classes, args to constructor arguments, and props to properties; hooks like useFrame, useThree and drei helpers cover loops, loaders and controls.
  • The mechanism is diff and patch: create objects once, then update only what changed, and dispose on unmount.
  • Mutate refs in per-frame code; use on-demand rendering for static scenes.
  • Learn plain Three.js first, then pick a framework when the app around the 3D scene calls for it.

Next: WebGPU, TSL and Build Tools