What is React Three Fiber?

Back to Modules

What is React Three Fiber (R3F)?

React Three Fiber (R3F) is a React renderer for Three.js, a powerful JavaScript 3D library. It allows developers to build 3D scenes and animations for the web and React Native applications using React's familiar component-based architecture and declarative syntax.

Key Aspects of React Three Fiber:

Why Use R3F?

R3F brings the benefits of React development (component reusability, declarative code, state management) to the world of 3D graphics. If you're already familiar with React, R3F significantly lowers the barrier to entry for creating complex and interactive 3D experiences on the web.

Basic R3F Example: A Rotating Box

Here's a simple example of a rotating box using React Three Fiber. Note that this requires a React environment to run.

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

function Box(props) {
  // This reference will give us direct access to the THREE.Mesh object
  const meshRef = useRef();
  // Subscribe this component to the render-loop, rotate the mesh every frame
  useFrame(() => {
    if (meshRef.current) {
      meshRef.current.rotation.x += 0.01;
      meshRef.current.rotation.y += 0.01;
    }
  });

  // Return the view, these are regular Three.js elements expressed in JSX
  return (
    <mesh {...props} ref={meshRef}>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={'hotpink'} />
    </mesh>
  );
}

export default function App() {
  return (
    <Canvas>
      <ambientLight />
      <pointLight position={[10, 10, 10]} />
      <Box position={[-1.2, 0, 0]} />
      <Box position={[1.2, 0, 0]} />
    </Canvas>
  );
}