React Three Fiber — Creative Web

R3F = three.js through JSX: <Canvas> sets up the scene, the camera (fov 75, z = 5), the renderer (dpr [1, 2], a transparent background), resizing, the loop…

Raka wants to order a shop sign. He can tell the craftsman step by step ("cut the wood, paint it brown, put the lettering on the left"): that's the imperative way. Or he hands over a drawing of the finished sign: the declarative way. When the price in the drawing changes, the craftsman only swaps the numbers.

The three.js code in the previous lessons is the imperative way. React is the declarative craftsman for the DOM, and React Three Fiber (R3F) is the same craftsman for three.js: a React renderer that builds the 3D scene from JSX components, then creates, updates and throws away the three.js objects itself. In this lesson Raka's display from the previous lesson (the mug and the shirt box) is rewritten with R3F, then compared with the plain three.js version.

Installing R3F

npm install three @react-three/fiber @react-three/drei

As of October 2026: @react-three/fiber 9.8.2 needs React 19 (peer react >=19 <19.4) and three 0.156 or later; @react-three/drei 10.7.9 needs fiber 9. Projects still on React 18 use R3F 8.

Canvas, mesh and JSX

import { Canvas } from '@react-three/fiber';

export default function Display() {
  return (
    <div style={{ width: 320, height: 240 }}>
      <Canvas camera={{ position: [0, 1.2, 5.2], fov: 40 }}>
        <color attach="background" args={['#f5e6c8']} />
        <ambientLight intensity={0.8} />
        <directionalLight position={[3, 4, 5]} intensity={2.5} />
        <mesh position={[1.2, 0, 0]}>
          <boxGeometry args={[1.6, 1, 0.4]} />
          <meshStandardMaterial color="#1c1917" roughness={0.5} />
        </mesh>
      </Canvas>
    </div>
  );
}
three.js R3F
new THREE.Mesh(geo, mat) then scene.add <mesh> inside <Canvas>
new THREE.BoxGeometry(1.6, 1, 0.4) <boxGeometry args={[1.6, 1, 0.4]} /> (the constructor's arguments)
mesh.position.set(1.2, 0, 0) position={[1.2, 0, 0]}
mug.add(handle) a <mesh> inside a <group>
mesh.material = [a, b, c] <meshStandardMaterial attach="material-0" /> and so on

Element names are three.js class names with a lowercase first letter. So <Canvas><mesh><boxGeometry /><meshStandardMaterial color="orange" /></mesh></Canvas> is an orange 3D cube: a mesh with a box geometry and an orange material. For the orange to show, add lights as above: with no light meshStandardMaterial is black (the Geometry & Material lesson), and from the default camera at (0, 0, 5) the cube only shows as a black square.

What Canvas sets up

<Canvas> makes the Scene, a PerspectiveCamera (by default fov 75 at z = 5, facing the centre) and a WebGLRenderer (antialias, a transparent background, dpr [1, 2]: devicePixelRatio kept between 1 and 2), runs the render loop, follows its container's size (aspect + updateProjectionMatrix, the Three.js Setup lesson), and throws objects away when their component goes. A texture passed through the map prop gets SRGBColorSpace by itself.

One thing that often surprises when the mug moves over from plain three.js with exactly the same light values, colours and camera: without the flat prop, Canvas sets ACESFilmicToneMapping, while plain three.js uses NoToneMapping. Measured in three.js 0.186 on the same display: the shirt box side goes from #1e1c1b to #0c0a09, the print cup from #a76835 to #b76f31; the background stays #f5e6c8. So it is not the same: darker and higher in contrast. <Canvas flat> makes them match.

useFrame and useThree

import { useRef } from 'react';
import { useFrame, useThree } from '@react-three/fiber';

function Mug(props) {
  const ref = useRef();
  const invalidate = useThree((state) => state.invalidate); // take a piece of R3F's state
  useFrame((state, delta) => {
    ref.current.rotation.y += 0.8 * delta;  // radians per second, the same at 60 and 120 Hz
  });
  return <group ref={ref} {...props}>{/* body + handle */}</group>;
}

Events: onClick and onPointerOver

<group
  onPointerOver={(e) => { e.stopPropagation(); setHighlighted(true); }}
  onPointerOut={() => setHighlighted(false)}
  onClick={(e) => { if (e.delta > 4) return; setGreen((g) => !g); }}
>

There is no raycaster in your code: R3F works out the pointer's NDC and runs three.js's Raycaster against the objects that have handlers, then calls the handler like a DOM event. e.delta is the distance in pixels since pointerdown, so clicks that were really drags can be filtered out. e.stopPropagation() stops the event reaching objects behind.

drei: ready-made helpers

@react-three/drei ("drei" = three in German) holds ready-made components for R3F:

import { OrbitControls, Html, useTexture } from '@react-three/drei';

<OrbitControls enableZoom={false} />           // damping already on, update() in its own useFrame
<Html position={[0, 1.15, 0]} center>Coffee Print Mug · $65</Html> // a DOM element that follows a 3D point
const map = useTexture('/coffee-print.png');   // load an image; the component waits inside <Suspense>

Others: Environment (light from an HDRI image), Text (text in the 3D scene), useGLTF (.glb models, covered in the next lesson).

frameloop="demand"

<Canvas frameloop="demand"> only renders when asked: a prop changes, invalidate() is called, or <OrbitControls> moves. While idle, zero renders a second, kind to the battery. The catch: useFrame sleeps too, so a mug that turns by itself stops; and the next frame's delta counts from the last frame that was rendered, so after 5 idle seconds a 0.8 rad/s mug jumps 4 radians. For continuous animation keep "always" (the default).

When R3F helps, and when it doesn't

⚠️ What often goes wrong when moving to R3F

TL;DR: R3F = three.js through JSX: <Canvas> sets up the scene, the camera (fov 75, z = 5), the renderer (dpr [1, 2], a transparent background), resizing, the loop and cleanup; elements = lowercase class names, args for the constructor's arguments, props for properties, attach="material-0" for arrays. useFrame((state, delta) => ...) every frame through a ref; useThree reads the state. onClick/onPointerOver events on a mesh: R3F runs the raycasting. drei: OrbitControls, Html, useTexture, Environment, Text. Canvas sets ACES tone mapping unless flat; frameloop="demand" saves battery but stops useFrame. R3F 9.8 needs React 19; for a page without React, plain three.js is lighter.

⚡ A scene from JSX

Write:

<Canvas camera={{ position: [0, 1.2, 5.2], fov: 40 }}>
  <ambientLight intensity={0.8} />
  <mesh position={[1.2, 0, 0]}>
    <boxGeometry args={[1.6, 1, 0.4]} />
    <meshStandardMaterial color="#1c1917" roughness={0.5} />
  </mesh>
</Canvas>

Meaning: R3F builds the three.js scene from JSX components. Element names are three.js class names with a lowercase first letter, and args holds the constructor's arguments. <Canvas> sets up the scene, the camera, the renderer, the sizing and the render loop.

⚡ Motion through useFrame

Write:

useFrame((state, delta) => {
  ref.current.rotation.y += 0.8 * delta;  // radians per second, the same at 60 and 120 Hz
});

Meaning: useFrame is called every frame before rendering. Change the object through a ref, not setState, so React doesn't re-render the component every frame.

⚡ drei and flat

Guess first: the mug moves over from plain three.js with exactly the same lights, colours and camera. Are the colours the same?

No. Without the flat prop, Canvas sets ACES tone mapping, so the colours come out darker and higher in contrast. <Canvas flat> makes them match.

Meaning: @react-three/drei holds ready-made components and helpers for R3F: OrbitControls, Html, useTexture, Environment, Text.

What you will learn