@react-three/fiber
A React renderer for Threejs
Last updated 3 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.

npm install three @react-three/fiber

Why?

Build your scene declaratively with re-usable, self-contained components that react to state, are readily interactive and can tap into React's ecosystem.

Does it have limitations?

None. Everything that works in threejs will work here without exception.

Can it keep up with frequent updates to threejs?

Yes, because it merely expresses threejs in JSX: <mesh /> becomes new THREE.Mesh(), and that happens dynamically. There is no hard dependency on a particular threejs version, it does not wrap or duplicate a single threejs class.

Is it slower than plain threejs?

There is no additional overhead. Components participate in the renderloop outside of React.

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).

Imports first

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

Define a component

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 += 0.01))

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

Compose the scene

Either use Canvas, which you can think of as a portal to threejs inside your regular dom graph. Everything within it is a native threejs element. If you want to mix Webgl and Html (react-dom) this is what you should use.

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'),
)

Or use react-three-fibers own render function, which is a little more low-level but could save you the extra cost of carrying react-dom. It renders into a dom canvas element. Use this for Webgl-only apps.

import { render } from '@react-three/fiber'

render(<Scene />, document.querySelector('canvas'))
<summary>Show TypeScript example</summary>
import { Canvas, MeshProps, useFrame } from '@react-three/fiber'

const Box: React.FC<MeshProps> = (props) => {
  // This reference will give us direct access to the mesh
  const mesh = useRef<THREE.Mesh>(null!)
  // 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 += 0.01))

  return (
    <mesh
      {...props}
      ref={mesh}
      scale={active ? 1.5 : 1}
      onClick={(event) => setActive(!active)}
      onPointerOver={(event) => setHover(true)}
      onPointerOut={(event) => setHover(false)}>
      <boxGeometry args={[1, 2, 3]} />
      <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'),
)

Documentation

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.

Backers

Thank you to all our backers! 🙏

Contributors

This project exists thanks to all the people who contribute.

Current Tags

  • 9.0.0-alpha.8                                ...           alpha (6 months ago)
  • 9.0.0-beta.1                                ...           beta (5 months ago)
  • 8.16.8                                ...           latest (5 months ago)

234 Versions

  • 9.0.0-beta.1                                ...           5 months ago
  • 8.16.8                                ...           5 months ago
  • 8.16.7                                ...           5 months ago
  • 9.0.0-beta.0                                ...           6 months ago
  • 9.0.0-alpha.8                                ...           6 months ago
  • 9.0.0-alpha.7                                ...           6 months ago
  • 8.16.6                                ...           6 months ago
  • 9.0.0-alpha.6                                ...           6 months ago
  • 8.16.5                                ...           6 months ago
  • 8.16.4                                ...           6 months ago
  • 9.0.0-alpha.5                                ...           6 months ago
  • 8.16.3                                ...           6 months ago
  • 9.0.0-alpha.4                                ...           7 months ago
  • 9.0.0-alpha.3                                ...           7 months ago
  • 8.16.2                                ...           7 months ago
  • 8.16.1                                ...           8 months ago
  • 8.16.0                                ...           8 months ago
  • 8.15.19                                ...           8 months ago
  • 8.15.18                                ...           8 months ago
  • 8.15.17                                ...           8 months ago
  • 8.15.16                                ...           9 months ago
  • 8.15.15                                ...           10 months ago
  • 8.15.14                                ...           10 months ago
  • 8.15.13                                ...           10 months ago
  • 8.15.12                                ...           a year ago
  • 8.15.11                                ...           a year ago
  • 8.15.10                                ...           a year ago
  • 8.15.9                                ...           a year ago
  • 8.15.8                                ...           a year ago
  • 8.15.7                                ...           a year ago
  • 8.15.6                                ...           a year ago
  • 8.15.5                                ...           a year ago
  • 8.15.4                                ...           a year ago
  • 8.15.3                                ...           a year ago
  • 8.15.2                                ...           a year ago
  • 8.15.1                                ...           a year ago
  • 8.15.0                                ...           a year ago
  • 8.14.7                                ...           a year ago
  • 8.14.6                                ...           a year ago
  • 8.14.5                                ...           a year ago
  • 8.14.4                                ...           a year ago
  • 8.14.3                                ...           a year ago
  • 8.14.2                                ...           a year ago
  • 8.14.1                                ...           a year ago
  • 8.14.0                                ...           a year ago
  • 8.13.9                                ...           a year ago
  • 8.13.8                                ...           a year ago
  • 8.13.7                                ...           a year ago
  • 8.13.6                                ...           a year ago
  • 8.13.5                                ...           a year ago
  • 8.13.4                                ...           a year ago
  • 8.13.3                                ...           a year ago
  • 8.13.2                                ...           a year ago
  • 8.13.1                                ...           a year ago
  • 8.13.0                                ...           2 years ago
  • 8.12.2                                ...           2 years ago
  • 8.12.1                                ...           2 years ago
  • 8.12.0                                ...           2 years ago
  • 8.11.11                                ...           2 years ago
  • 8.11.10                                ...           2 years ago
  • 8.11.9                                ...           2 years ago
  • 8.11.8                                ...           2 years ago
  • 8.11.7                                ...           2 years ago
  • 8.11.6                                ...           2 years ago
  • 8.11.5                                ...           2 years ago
  • 8.11.4                                ...           2 years ago
  • 8.11.3                                ...           2 years ago
  • 8.11.2                                ...           2 years ago
  • 8.11.1                                ...           2 years ago
  • 8.11.0                                ...           2 years ago
  • 8.10.4                                ...           2 years ago
  • 8.10.3                                ...           2 years ago
  • 8.10.2                                ...           2 years ago
  • 8.10.1                                ...           2 years ago
  • 8.10.0                                ...           2 years ago
  • 8.9.2                                ...           2 years ago
  • 8.9.1                                ...           2 years ago
  • 8.9.0                                ...           2 years ago
  • 8.8.11                                ...           2 years ago
  • 8.8.10                                ...           2 years ago
  • 8.8.9                                ...           2 years ago
  • 8.8.8                                ...           2 years ago
  • 8.8.7                                ...           2 years ago
  • 8.8.6                                ...           2 years ago
  • 8.8.5                                ...           2 years ago
  • 8.8.4                                ...           2 years ago
  • 8.8.3                                ...           2 years ago
  • 9.0.0-alpha.2                                ...           2 years ago
  • 8.8.2                                ...           2 years ago
  • 8.8.1                                ...           2 years ago
  • 8.8.0                                ...           2 years ago
  • 8.7.4                                ...           2 years ago
  • 8.7.3                                ...           2 years ago
  • 8.7.2                                ...           2 years ago
  • 8.7.1                                ...           2 years ago
  • 8.7.0                                ...           2 years ago
  • 8.6.2                                ...           2 years ago
  • 8.6.1                                ...           2 years ago
  • 8.6.0                                ...           2 years ago
  • 8.5.1                                ...           2 years ago
  • 8.5.0                                ...           2 years ago
  • 8.4.1                                ...           2 years ago
  • 8.4.0                                ...           2 years ago
  • 9.0.0-alpha.1                                ...           2 years ago
  • 8.3.1                                ...           2 years ago
  • 8.3.0                                ...           2 years ago
  • 8.2.3                                ...           2 years ago
  • 9.0.0-alpha.0                                ...           2 years ago
  • 8.2.2                                ...           2 years ago
  • 8.2.1                                ...           2 years ago
  • 8.2.0                                ...           2 years ago
  • 8.1.0                                ...           2 years ago
  • 8.0.27                                ...           2 years ago
  • 8.0.26                                ...           2 years ago
  • 8.0.25                                ...           2 years ago
  • 8.0.24                                ...           2 years ago
  • 8.0.23                                ...           2 years ago
  • 8.0.22                                ...           2 years ago
  • 8.0.21                                ...           2 years ago
  • 8.0.20                                ...           2 years ago
  • 8.0.19                                ...           2 years ago
  • 8.0.18                                ...           2 years ago
  • 8.0.17                                ...           2 years ago
  • 7.0.29                                ...           2 years ago
  • 8.0.16                                ...           3 years ago
  • 8.0.15                                ...           3 years ago
  • 8.0.14                                ...           3 years ago
  • 7.0.28                                ...           3 years ago
  • 8.0.13                                ...           3 years ago
  • 8.0.12                                ...           3 years ago
  • 8.0.11                                ...           3 years ago
  • 8.0.10                                ...           3 years ago
  • 8.0.9                                ...           3 years ago
  • 8.0.8                                ...           3 years ago
  • 8.0.7                                ...           3 years ago
  • 8.0.6                                ...           3 years ago
  • 8.0.5                                ...           3 years ago
  • 8.0.4                                ...           3 years ago
  • 8.0.3                                ...           3 years ago
  • 8.0.2                                ...           3 years ago
  • 8.0.1                                ...           3 years ago
  • 8.0.0                                ...           3 years ago
  • 7.0.27                                ...           3 years ago
  • 8.0.0-beta.10                                ...           3 years ago
  • 8.0.0-beta.9                                ...           3 years ago
  • 8.0.0-beta.8                                ...           3 years ago
  • 8.0.0-beta.7                                ...           3 years ago
  • 8.0.0-beta.6                                ...           3 years ago
  • 8.0.0-beta.5                                ...           3 years ago
  • 8.0.0-beta.4                                ...           3 years ago
  • 8.0.0-beta.3                                ...           3 years ago
  • 8.0.0-beta.2                                ...           3 years ago
  • 8.0.0-beta.1                                ...           3 years ago
  • 8.0.0-beta.0                                ...           3 years ago
  • 7.0.26                                ...           3 years ago
  • 8.0.0-beta-05                                ...           3 years ago
  • 7.0.25                                ...           3 years ago
  • 7.0.24                                ...           3 years ago
  • 8.0.0-beta-04                                ...           3 years ago
  • 8.0.0-beta-03                                ...           3 years ago
  • 7.0.23                                ...           3 years ago
  • 7.0.22                                ...           3 years ago
  • 7.0.21                                ...           3 years ago
  • 7.0.20                                ...           3 years ago
  • 8.0.0-beta-02                                ...           3 years ago
  • 8.0.0-beta-01                                ...           3 years ago
  • 7.0.19                                ...           3 years ago
  • 7.0.18                                ...           3 years ago
  • 7.0.17                                ...           3 years ago
  • 7.0.16                                ...           3 years ago
  • 7.0.15                                ...           3 years ago
  • 7.0.14                                ...           3 years ago
  • 7.0.13                                ...           3 years ago
  • 7.0.12                                ...           3 years ago
  • 7.0.11                                ...           3 years ago
  • 7.0.10                                ...           3 years ago
  • 7.0.9                                ...           3 years ago
  • 7.0.8                                ...           3 years ago
  • 8.0.0-alpha-09                                ...           3 years ago
  • 8.0.0-alpha-08                                ...           3 years ago
  • 7.0.7                                ...           3 years ago
  • 8.0.0-alpha-07                                ...           3 years ago
  • 8.0.0-alpha-06                                ...           3 years ago
  • 8.0.0-alpha-05                                ...           3 years ago
  • 8.0.0-alpha-04                                ...           3 years ago
  • 8.0.0-alpha-03                                ...           3 years ago
  • 8.0.0-alpha-02                                ...           3 years ago
  • 8.0.0-alpha-01                                ...           3 years ago
  • 8.0.0-alpha.0                                ...           3 years ago
  • 7.0.6                                ...           3 years ago
  • 7.0.5                                ...           3 years ago
  • 7.0.4                                ...           3 years ago
  • 7.0.3                                ...           3 years ago
  • 7.0.2                                ...           3 years ago
  • 7.0.1                                ...           3 years ago
  • 7.0.0                                ...           3 years ago
  • 6.2.3                                ...           3 years ago
  • 6.2.2                                ...           3 years ago
  • 6.2.1                                ...           3 years ago
  • 6.2.0                                ...           3 years ago
  • 6.1.5                                ...           3 years ago
  • 6.1.4                                ...           3 years ago
  • 6.1.3                                ...           3 years ago
  • 6.1.2                                ...           4 years ago
  • 6.1.1                                ...           4 years ago
  • 6.1.0                                ...           4 years ago
  • 6.0.21                                ...           4 years ago
  • 6.0.20                                ...           4 years ago
  • 6.0.19                                ...           4 years ago
  • 6.0.18                                ...           4 years ago
  • 6.0.17                                ...           4 years ago
  • 6.0.16                                ...           4 years ago
  • 6.0.15                                ...           4 years ago
  • 6.0.14                                ...           4 years ago
  • 6.0.13                                ...           4 years ago
  • 6.0.12                                ...           4 years ago
  • 6.0.11                                ...           4 years ago
  • 6.0.10                                ...           4 years ago
  • 6.0.9                                ...           4 years ago
  • 6.0.8                                ...           4 years ago
  • 6.0.7                                ...           4 years ago
  • 6.0.6                                ...           4 years ago
  • 6.0.5                                ...           4 years ago
  • 6.0.4                                ...           4 years ago
  • 6.0.3                                ...           4 years ago
  • 6.0.2                                ...           4 years ago
  • 6.0.1                                ...           4 years ago
  • 6.0.0                                ...           4 years ago
  • 6.0.0-alpha.15                                ...           4 years ago
  • 6.0.0-alpha.14                                ...           4 years ago
  • 6.0.0-alpha.13                                ...           4 years ago
  • 6.0.0-alpha.12                                ...           4 years ago
  • 6.0.0-alpha.11                                ...           4 years ago
  • 5.3.15                                ...           4 years ago
Downloads
Total 2
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (15)
Dev Dependencies (0)
None
Dependents (1)

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