react-three-fiber
A React renderer for Three.js (web and react-native)
Last updated 4 years ago by drcmda .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install react-three-fiber 
SYNC missed versions from official npm registry.

react-three-fiber

Version Downloads Twitter Discord Open Collective ETH BTC

react-three-fiber is a React renderer for threejs on the web and react-native.

npm install three react-three-fiber

These demos are real, you can click them! They contain the full code, too.

Why?

Building dynamic scene graphs declaratively with re-usable components makes dealing with threejs easier and brings order and sanity to your codebase. These components react to state changes, are interactive out of the box and can tap into React's infinite ecosystem.

Does it have limitations?

None. Everything that works in threejs will work here. In contrast to "bindings" where a library ships/maintains dozens of wrapper components, it just renders JSX to threejs dynamically: <mesh /> simply is another expression for new THREE.Mesh(). It does not know or target a specific threejs version nor does it need updates for modified, added or removed upstream features.

Is it slower than raw threejs?

No. Rendering performance is up to threejs and the GPU. Components participate in the renderloop outside of React, without any additional overhead. React is otherwise very efficient in building and managing component-trees, it could potentially outperform manual/imperative apps at scale.

What does it look like?

Let's make a re-usable component that has its own state, reacts to user-input and participates in the render-loop. (live demo).
import ReactDOM from 'react-dom'
import React, { useRef, useState } from 'react'
import { Canvas, useFrame } from 'react-three-fiber'

function Box(props) {
  // This reference will give us direct access to the mesh
  const mesh = useRef()

  // Set up state for the hovered and active state
  const [hovered, setHover] = useState(false)
  const [active, setActive] = useState(false)

  // Rotate mesh every frame, this is outside of React without overhead
  useFrame(() => {
    mesh.current.rotation.x = mesh.current.rotation.y += 0.01
  })

  return (
    <mesh
      {...props}
      ref={mesh}
      scale={active ? [1.5, 1.5, 1.5] : [1, 1, 1]}
      onClick={(event) => setActive(!active)}
      onPointerOver={(event) => setHover(true)}
      onPointerOut={(event) => setHover(false)}>
      <boxBufferGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  )
}

ReactDOM.render(
  <Canvas>
    <ambientLight />
    <pointLight position={[10, 10, 10]} />
    <Box position={[-1.2, 0, 0]} />
    <Box position={[1.2, 0, 0]} />
  </Canvas>,
  document.getElementById('root')
)
<summary>Show TypeScript example</summary>
import ReactDOM from 'react-dom'
import React, { useRef, useState } from 'react'
import { Canvas, MeshProps, useFrame } from 'react-three-fiber'
import type { Mesh } from 'three'

const Box: React.FC<MeshProps> = (props) => {
  // This reference will give us direct access to the mesh
  const mesh = useRef<Mesh>()

  // Set up state for the hovered and active state
  const [hovered, setHover] = useState(false)
  const [active, setActive] = useState(false)

  // Rotate mesh every frame, this is outside of React without overhead
  useFrame(() => {
    if (mesh.current) mesh.current.rotation.x = mesh.current.rotation.y += 0.01
  })

  return (
    <mesh
      {...props}
      ref={mesh}
      scale={active ? [1.5, 1.5, 1.5] : [1, 1, 1]}
      onClick={(event) => setActive(!active)}
      onPointerOver={(event) => setHover(true)}
      onPointerOut={(event) => setHover(false)}>
      <boxBufferGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  )
}

ReactDOM.render(
  <Canvas>
    <ambientLight />
    <pointLight position={[10, 10, 10]} />
    <Box position={[-1.2, 0, 0]} />
    <Box position={[1.2, 0, 0]} />
  </Canvas>,
  document.getElementById('root')
)

API

Fundamentals

You need to be versed in both React and Threejs before rushing into this. If you are unsure about React consult the official React docs, especially the section about hooks. As for Threejs, make sure you at least glance over the following links:

  1. Make sure you have a basic grasp of Threejs. Keep that site open.
  2. When you know what a scene is, a camera, mesh, geometry, material, fork the demo above.
  3. Look up the JSX elements that you see (mesh, ambientLight, etc), all threejs exports are native to three-fiber.
  4. Try changing some values, scroll though our Api to see what the various settings and hooks do.

Some reading material:

Ecosystem

How to contribute

If you like this project, please consider helping out. All contributions are welcome as well as donations to Opencollective, or in crypto BTC: 36fuguTPxGCNnYZSRdgdh6Ea94brCAjMbH, ETH: 0x6E3f79Ea1d0dcedeb33D3fC6c34d2B1f156F2682.

Current Tags

  • 6.0.0-alpha.11                                ...           alpha (4 years ago)
  • 5.0.0-beta.12                                ...           beta (4 years ago)
  • 0.0.0-deprecated                                ...           deprecated (4 years ago)
  • 6.0.13                                ...           latest (4 years ago)

284 Versions

  • 6.0.13 [deprecated]           ...           4 years ago
  • 6.0.12 [deprecated]           ...           4 years ago
  • 6.0.11 [deprecated]           ...           4 years ago
  • 6.0.10 [deprecated]           ...           4 years ago
  • 5.3.22 [deprecated]           ...           4 years ago
  • 5.3.21 [deprecated]           ...           4 years ago
  • 5.3.20 [deprecated]           ...           4 years ago
  • 0.0.0-deprecated                                ...           4 years ago
  • 6.0.0-alpha.11                                ...           4 years ago
  • 6.0.0-alpha.10                                ...           4 years ago
  • 6.0.0-alpha.9                                ...           4 years ago
  • 6.0.0-alpha.8                                ...           4 years ago
  • 6.0.0-alpha.7                                ...           4 years ago
  • 6.0.0-alpha.6                                ...           4 years ago
  • 6.0.0-alpha.5                                ...           4 years ago
  • 6.0.0-alpha.4                                ...           4 years ago
  • 6.0.0-alpha.3                                ...           4 years ago
  • 6.0.0-alpha.2                                ...           4 years ago
  • 6.0.0-alpha.1                                ...           4 years ago
  • 6.0.0-alpha.0                                ...           4 years ago
  • 5.3.19 [deprecated]           ...           4 years ago
  • 5.3.18 [deprecated]           ...           4 years ago
  • 5.3.17 [deprecated]           ...           4 years ago
  • 5.3.16 [deprecated]           ...           4 years ago
  • 5.3.15 [deprecated]           ...           4 years ago
  • 5.3.14 [deprecated]           ...           4 years ago
  • 5.3.13 [deprecated]           ...           4 years ago
  • 5.3.12 [deprecated]           ...           4 years ago
  • 5.3.11 [deprecated]           ...           4 years ago
  • 5.3.10 [deprecated]           ...           4 years ago
  • 5.3.9 [deprecated]           ...           4 years ago
  • 5.3.8 [deprecated]           ...           4 years ago
  • 5.3.7 [deprecated]           ...           4 years ago
  • 5.3.6 [deprecated]           ...           4 years ago
  • 5.3.5 [deprecated]           ...           4 years ago
  • 5.3.4 [deprecated]           ...           4 years ago
  • 5.3.3 [deprecated]           ...           4 years ago
  • 5.3.1 [deprecated]           ...           4 years ago
  • 5.3.0 [deprecated]           ...           4 years ago
  • 5.2.1 [deprecated]           ...           4 years ago
  • 5.2.0 [deprecated]           ...           4 years ago
  • 5.1.9 [deprecated]           ...           4 years ago
  • 5.1.8 [deprecated]           ...           4 years ago
  • 5.1.7 [deprecated]           ...           4 years ago
  • 5.1.6 [deprecated]           ...           4 years ago
  • 5.1.5 [deprecated]           ...           4 years ago
  • 5.1.4 [deprecated]           ...           4 years ago
  • 5.1.3 [deprecated]           ...           4 years ago
  • 5.1.2 [deprecated]           ...           4 years ago
  • 5.1.1 [deprecated]           ...           4 years ago
  • 5.1.0 [deprecated]           ...           4 years ago
  • 5.0.6 [deprecated]           ...           4 years ago
  • 5.0.5 [deprecated]           ...           4 years ago
  • 5.0.4 [deprecated]           ...           4 years ago
  • 5.0.3 [deprecated]           ...           4 years ago
  • 5.0.2 [deprecated]           ...           4 years ago
  • 5.0.1 [deprecated]           ...           4 years ago
  • 5.0.0 [deprecated]           ...           4 years ago
  • 5.0.0-beta.12                                ...           4 years ago
  • 4.2.21 [deprecated]           ...           4 years ago
  • 4.2.20 [deprecated]           ...           4 years ago
  • 5.0.0-alpha.1                                ...           4 years ago
  • 4.2.19 [deprecated]           ...           4 years ago
  • 4.2.18 [deprecated]           ...           4 years ago
  • 5.0.0-alpha.0                                ...           4 years ago
  • 5.0.0-beta.11                                ...           4 years ago
  • 4.2.17 [deprecated]           ...           4 years ago
  • 4.2.16 [deprecated]           ...           4 years ago
  • 4.2.15 [deprecated]           ...           4 years ago
  • 4.2.14 [deprecated]           ...           4 years ago
  • 4.2.13 [deprecated]           ...           4 years ago
  • 4.2.12 [deprecated]           ...           5 years ago
  • 4.2.11 [deprecated]           ...           5 years ago
  • 5.0.0-beta.10                                ...           5 years ago
  • 4.2.10 [deprecated]           ...           5 years ago
  • 4.2.9 [deprecated]           ...           5 years ago
  • 5.0.0-beta.8                                ...           5 years ago
  • 5.0.0-beta.7                                ...           5 years ago
  • 5.0.0-beta.6                                ...           5 years ago
  • 5.0.0-beta.5                                ...           5 years ago
  • 5.0.0-beta.4                                ...           5 years ago
  • 4.2.8 [deprecated]           ...           5 years ago
  • 5.0.0-beta.3                                ...           5 years ago
  • 5.0.0-beta.2                                ...           5 years ago
  • 5.0.0-beta.1                                ...           5 years ago
  • 4.2.7 [deprecated]           ...           5 years ago
  • 4.2.6 [deprecated]           ...           5 years ago
  • 5.0.0-beta.0                                ...           5 years ago
  • 4.2.5 [deprecated]           ...           5 years ago
  • 4.2.4 [deprecated]           ...           5 years ago
  • 4.2.3 [deprecated]           ...           5 years ago
  • 4.2.2 [deprecated]           ...           5 years ago
  • 4.2.0 [deprecated]           ...           5 years ago
  • 4.1.4 [deprecated]           ...           5 years ago
  • 4.1.3 [deprecated]           ...           5 years ago
  • 4.1.2 [deprecated]           ...           5 years ago
  • 4.1.1 [deprecated]           ...           5 years ago
  • 4.1.0 [deprecated]           ...           5 years ago
  • 4.0.29-beta.0                                ...           5 years ago
  • 4.0.28 [deprecated]           ...           5 years ago
  • 4.0.27 [deprecated]           ...           5 years ago
  • 4.0.26 [deprecated]           ...           5 years ago
  • 4.0.25 [deprecated]           ...           5 years ago
  • 4.0.24 [deprecated]           ...           5 years ago
  • 4.0.23 [deprecated]           ...           5 years ago
  • 4.0.22 [deprecated]           ...           5 years ago
  • 4.0.21 [deprecated]           ...           5 years ago
  • 4.0.21-beta.1                                ...           5 years ago
  • 4.0.21-beta.0                                ...           5 years ago
  • 4.0.20 [deprecated]           ...           5 years ago
  • 4.0.19 [deprecated]           ...           5 years ago
  • 4.0.18 [deprecated]           ...           5 years ago
  • 4.0.17 [deprecated]           ...           5 years ago
  • 4.0.16 [deprecated]           ...           5 years ago
  • 4.0.14 [deprecated]           ...           5 years ago
  • 4.0.13 [deprecated]           ...           5 years ago
  • 4.0.12 [deprecated]           ...           5 years ago
  • 4.0.11 [deprecated]           ...           5 years ago
  • 4.0.10 [deprecated]           ...           5 years ago
  • 4.0.9 [deprecated]           ...           5 years ago
  • 4.0.8 [deprecated]           ...           5 years ago
  • 4.0.7 [deprecated]           ...           5 years ago
  • 4.0.6 [deprecated]           ...           5 years ago
  • 4.0.5 [deprecated]           ...           5 years ago
  • 4.0.4 [deprecated]           ...           5 years ago
  • 4.0.3 [deprecated]           ...           5 years ago
  • 4.0.2 [deprecated]           ...           5 years ago
  • 4.0.1 [deprecated]           ...           5 years ago
  • 4.0.0-beta.16                                ...           5 years ago
  • 4.0.0-beta.15                                ...           5 years ago
  • 4.0.0-beta.14                                ...           5 years ago
  • 3.0.17 [deprecated]           ...           5 years ago
  • 4.0.0-beta.13                                ...           5 years ago
  • 4.0.0-beta.12                                ...           5 years ago
  • 3.0.16 [deprecated]           ...           5 years ago
  • 4.0.0-beta.11                                ...           5 years ago
  • 4.0.0-beta.10                                ...           5 years ago
  • 4.0.0-beta.9                                ...           5 years ago
  • 4.0.0-beta.8                                ...           5 years ago
  • 4.0.0-beta.7                                ...           5 years ago
  • 4.0.0-beta.6                                ...           5 years ago
  • 4.0.0-beta.5                                ...           5 years ago
  • 4.0.0-beta.4                                ...           5 years ago
  • 4.0.0-beta.3                                ...           5 years ago
  • 4.0.0-beta.2                                ...           5 years ago
  • 4.0.0-beta.1                                ...           5 years ago
  • 4.0.0-beta.0                                ...           5 years ago
  • 3.0.15 [deprecated]           ...           5 years ago
  • 3.0.14 [deprecated]           ...           5 years ago
  • 3.0.13 [deprecated]           ...           5 years ago
  • 3.0.12 [deprecated]           ...           5 years ago
  • 3.0.11 [deprecated]           ...           5 years ago
  • 3.0.10 [deprecated]           ...           5 years ago
  • 3.0.9 [deprecated]           ...           5 years ago
  • 3.0.8-beta.0                                ...           5 years ago
  • 3.0.8 [deprecated]           ...           5 years ago
  • 3.0.7 [deprecated]           ...           5 years ago
  • 3.0.6 [deprecated]           ...           5 years ago
  • 3.0.3 [deprecated]           ...           5 years ago
  • 3.0.2 [deprecated]           ...           5 years ago
  • 3.0.1 [deprecated]           ...           5 years ago
  • 3.0.0 [deprecated]           ...           5 years ago
  • 3.0.0-beta.16                                ...           5 years ago
  • 3.0.0-beta.15                                ...           5 years ago
  • 3.0.0-beta.14                                ...           5 years ago
  • 3.0.0-beta.13                                ...           5 years ago
  • 3.0.0-beta.12                                ...           5 years ago
  • 3.0.0-beta.11                                ...           5 years ago
  • 3.0.0-beta.10                                ...           5 years ago
  • 3.0.0-beta.9                                ...           5 years ago
  • 3.0.0-beta.8                                ...           5 years ago
  • 3.0.0-beta.7                                ...           5 years ago
  • 3.0.0-beta.6                                ...           5 years ago
  • 3.0.0-beta.4                                ...           5 years ago
  • 3.0.0-beta.3                                ...           5 years ago
  • 3.0.0-beta.1                                ...           5 years ago
  • 3.0.0-beta.0                                ...           5 years ago
  • 2.5.0-beta.1                                ...           5 years ago
  • 2.5.0-beta.0                                ...           5 years ago
  • 2.4.3 [deprecated]           ...           5 years ago
  • 2.4.2 [deprecated]           ...           5 years ago
  • 2.4.1 [deprecated]           ...           5 years ago
  • 2.4.0 [deprecated]           ...           5 years ago
  • 2.3.9 [deprecated]           ...           5 years ago
  • 2.3.8 [deprecated]           ...           5 years ago
  • 2.3.7 [deprecated]           ...           5 years ago
  • 2.3.6 [deprecated]           ...           5 years ago
  • 2.3.5 [deprecated]           ...           5 years ago
  • 2.3.4 [deprecated]           ...           5 years ago
  • 2.3.3-beta.0                                ...           5 years ago
  • 2.3.3 [deprecated]           ...           5 years ago
  • 2.3.2 [deprecated]           ...           5 years ago
  • 2.3.1 [deprecated]           ...           5 years ago
  • 2.3.0 [deprecated]           ...           5 years ago
  • 2.2.17-beta.2                                ...           5 years ago
  • 2.2.17-beta.1                                ...           5 years ago
  • 2.2.17-beta.0                                ...           5 years ago
  • 2.2.17 [deprecated]           ...           5 years ago
  • 2.2.16 [deprecated]           ...           5 years ago
  • 2.2.15 [deprecated]           ...           5 years ago
  • 2.2.14 [deprecated]           ...           5 years ago
  • 2.2.13 [deprecated]           ...           5 years ago
  • 2.2.12 [deprecated]           ...           5 years ago
  • 2.2.11 [deprecated]           ...           5 years ago
  • 2.2.10 [deprecated]           ...           5 years ago
  • 2.2.9 [deprecated]           ...           6 years ago
  • 2.2.8 [deprecated]           ...           6 years ago
  • 2.2.7 [deprecated]           ...           6 years ago
  • 2.2.6 [deprecated]           ...           6 years ago
  • 2.2.5 [deprecated]           ...           6 years ago
  • 2.2.4 [deprecated]           ...           6 years ago
  • 2.2.3 [deprecated]           ...           6 years ago
  • 2.2.2 [deprecated]           ...           6 years ago
  • 2.2.1 [deprecated]           ...           6 years ago
  • 2.2.0 [deprecated]           ...           6 years ago
  • 2.1.6 [deprecated]           ...           6 years ago
  • 2.1.5 [deprecated]           ...           6 years ago
  • 2.1.4 [deprecated]           ...           6 years ago
  • 2.1.3-beta.0                                ...           6 years ago
  • 2.1.3 [deprecated]           ...           6 years ago
  • 2.1.2 [deprecated]           ...           6 years ago
  • 2.1.1 [deprecated]           ...           6 years ago
  • 2.1.0 [deprecated]           ...           6 years ago
  • 2.1.0-beta.2                                ...           6 years ago
  • 2.1.0-beta.1                                ...           6 years ago
  • 2.1.0-beta.0                                ...           6 years ago
  • 2.0.12 [deprecated]           ...           6 years ago
  • 2.0.11 [deprecated]           ...           6 years ago
  • 2.0.10 [deprecated]           ...           6 years ago
  • 2.0.9 [deprecated]           ...           6 years ago
  • 2.0.8 [deprecated]           ...           6 years ago
  • 2.0.7 [deprecated]           ...           6 years ago
  • 2.0.6 [deprecated]           ...           6 years ago
  • 2.0.5 [deprecated]           ...           6 years ago
  • 2.0.4 [deprecated]           ...           6 years ago
  • 2.0.3 [deprecated]           ...           6 years ago
  • 2.0.2 [deprecated]           ...           6 years ago
  • 2.0.1 [deprecated]           ...           6 years ago
  • 2.0.0 [deprecated]           ...           6 years ago
  • 2.0.0-beta.12                                ...           6 years ago
  • 2.0.0-beta.11                                ...           6 years ago
  • 2.0.0-beta.10                                ...           6 years ago
  • 2.0.0-beta.9                                ...           6 years ago
  • 2.0.0-beta.8                                ...           6 years ago
  • 2.0.0-beta.7                                ...           6 years ago
  • 2.0.0-beta.6                                ...           6 years ago
  • 2.0.0-beta.5                                ...           6 years ago
  • 2.0.0-beta.4                                ...           6 years ago
  • 2.0.0-beta.3                                ...           6 years ago
  • 2.0.0-beta.2                                ...           6 years ago
  • 2.0.0-beta.1                                ...           6 years ago
  • 1.5.4-beta.0                                ...           6 years ago
  • 1.5.3 [deprecated]           ...           6 years ago
  • 1.5.2 [deprecated]           ...           6 years ago
  • 1.5.1 [deprecated]           ...           6 years ago
  • 1.5.0 [deprecated]           ...           6 years ago
  • 1.4.0-beta.1                                ...           6 years ago
  • 1.4.0-beta.0                                ...           6 years ago
  • 1.3.10 [deprecated]           ...           6 years ago
  • 1.3.9-beta.1                                ...           6 years ago
  • 1.3.9 [deprecated]           ...           6 years ago
  • 1.3.8 [deprecated]           ...           6 years ago
  • 1.3.7 [deprecated]           ...           6 years ago
  • 1.3.6 [deprecated]           ...           6 years ago
  • 1.3.5 [deprecated]           ...           6 years ago
  • 1.3.4 [deprecated]           ...           6 years ago
  • 1.3.3 [deprecated]           ...           6 years ago
  • 1.3.2 [deprecated]           ...           6 years ago
  • 1.3.1 [deprecated]           ...           6 years ago
  • 1.3.0-beta.0                                ...           6 years ago
  • 1.3.0 [deprecated]           ...           6 years ago
  • 1.2.7 [deprecated]           ...           6 years ago
  • 1.2.4 [deprecated]           ...           6 years ago
  • 1.2.2 [deprecated]           ...           6 years ago
  • 1.2.1 [deprecated]           ...           6 years ago
  • 1.2.0 [deprecated]           ...           6 years ago
  • 1.1.0 [deprecated]           ...           6 years ago
  • 1.0.8 [deprecated]           ...           6 years ago
  • 1.0.7 [deprecated]           ...           6 years ago
  • 1.0.6 [deprecated]           ...           6 years ago
  • 1.0.5 [deprecated]           ...           6 years ago
  • 1.0.4 [deprecated]           ...           6 years ago
  • 1.0.3 [deprecated]           ...           6 years ago
  • 1.0.2 [deprecated]           ...           6 years ago
Maintainers (1)
Downloads
Total 3
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (10)
Dev Dependencies (63)

© 2010 - cnpmjs.org x YWFE | Home | YWFE