Blog Logo
TAGS

@react-three/fiber

react-three-fiber is a React renderer for threejs. Build your scene declaratively with re-usable, self-contained components that react to state, are readily interactive and can participate in Reacts ecosystem. npm install three @types/three @react-three/fiber. Does it have limitations? None. Everything that works in Threejs will work here without exception. Is it slower than plain Threejs? No. There is no overhead. Components render outside of React. It outperforms Threejs in scale due to Reacts scheduling abilities. Can it keep up with frequent feature updates to Threejs? Yes. It merely expresses Threejs in JSX, dynamically turns into new THREE.Mesh(). If a new Threejs version adds, removes or changes features, it will be available to you instantly without depending on updates to this library. What does it look like? Lets make a re-usable component that has its own state, reacts to user-input and participates in the render-loop. (live demo). npm install @types/three. import * as THREE from three. import { createRoot } from react-dom/client. import React, { useRef, useState } from react. import { Canvas, useFrame, ThreeElements } from @react-three/fiber. function Box(props: ThreeElements[mesh]) { const ref = useRef(null!). const [hovered, hover] = useState(false). const [clicked, click] = useState(false). useFrame((state, delta) => (ref.current.rotation.x += delta)). return ( click(!clicked)} onPointerOver={(event) => hover(true)} onPointerOut={(event) => hover(false)}>)}