# Index
- [Fundamentals](#fundamentals)
- [Canvas](#canvas)
- [Objects and properties](#objects-and-properties)
- [Automatic disposal](#automatic-disposal)
- [Events](#events)
- [Hooks](#hooks)
- [useThree](#useThree)
- [useFrame](#useFrame)
- [useResource](#useResource)
- [useUpdate](#useUpdate)
- [useLoader (experimental!)](#useloader-experimental)
- [Additional exports](#additional-exports)
# Fundamentals
1. Before you start, make sure you have a [basic grasp of Threejs](https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene).
2. When you know what a scene is, a camera, mesh, geometry and material, more or less, fork the [demo sandbox on the frontpage](https://github.com/react-spring/react-three-fiber#what-does-it-look-like), try out some of the things you learn here.
3. Don't break your head, three-fiber is Threejs, it does not introduce new rules or assumptions. If you see a snippet somewhere and you don't know how to make it declarative yet, use it 1:1 as it is.
Some reading material:
- [Threejs-docs](https://threejs.org/docs)
- [Threejs-examples](https://threejs.org/examples)
- [Threejs-fundamentals](https://threejsfundamentals.org)
- [Discover Threejs](https://discoverthreejs.com)
- [Do's and don'ts](https://discoverthreejs.com/tips-and-tricks) for performance and best practices
- [react-three-fiber alligator.io tutorial](https://alligator.io/react/react-with-threejs) by [@dghez\_](https://twitter.com/dghez_)
# Canvas
The `Canvas` object is your portal into Threejs. It renders Threejs elements, _not DOM elements_! Here is a small hello-world that you can try out:
```jsx
import ReactDOM from 'react-dom'
import React from 'react'
import { Canvas } from 'react-three-fiber'
ReactDOM.render(
,
document.getElementById('root')
)
```
The canvas stretches to 100% of the next relative/absolute parent-container. Make sure your canvas is given space to show contents!
```jsx
// Response for pointer clicks that have missed a target
```
You can give it additional properties like style and className, which will be added to the container (a div) that holds the dom-canvas element.
### Defaults that the canvas component sets up
Canvas will create a _translucent WebGL-renderer_ with the following properties: `antialias, alpha, setClearAlpha(0)`
A default _perspective camera_: `fov: 75, near: 0.1, far: 1000, z: 5, lookAt: [0,0,0]`
A default _orthographic camera_ if Canvas.orthographic is true: `near: 0.1, far: 1000, z: 5, lookAt: [0,0,0]`
A default _shadowMap_ if Canvas.shadowMap is true: `type: PCFSoftShadowMap`
A default _scene_ (into which all the JSX is rendered) and a _raycaster_.
A _wrapping container_ with a [resize observer](https://github.com/react-spring/react-use-measure): `scroll: true, debounce: { scroll: 50, resize: 0 }`
You do not have to use any of these objects, look under "Recipes" down below if you want to bring your own.
# Objects and properties
You can use [Threejs's entire object catalogue and all properties](https://threejs.org/docs). When in doubt, always consult the docs.
You could lay out an object like this:
```jsx
```
The problem is that all of these properties will always be re-created. Instead, you should define properties declaratively.
```jsx
```
#### Shortcuts (set)
All properties whose underlying object has a `.set()` method can directly receive the same arguments that `set` would otherwise take. For example [THREE.Color.set](https://threejs.org/docs/index.html#api/en/math/Color.set) can take a color string, so instead of `color={new THREE.Color('hotpink')}` you can simply write `color="hotpink"`. Some `set` methods take multiple arguments, for instance [THREE.Vector3](https://threejs.org/docs/index.html#api/en/math/Vector3.set), give it an array in that case `position={[100, 0, 0]}`.
#### Dealing with non-Object3D's
You can put non-Object3D primitives (geometries, materials, etc) into the render tree as well, so that they become managed and reactive. They take the same properties they normally would, constructor arguments are passed as an array via `args`. If args changes later on, the object get re-constructed from scratch! Using the `attach` property objects bind to their parent and are taken off once they unmount.
You can nest primitive objects, too:
```jsx
img && (self.needsUpdate = true)} />
```
Sometimes attaching isn't enough. For example, the following example attaches effects to an array called "passes" of the parent `effectComposer`. Note the use of `attachArray` which adds the object to the target array and takes it out on unmount:
```jsx
```
You can also attach to named parent properties using `attachObject={[target, name]}`, which adds the object and takes it out on unmount. The following adds a buffer-attribute to parent.attributes.position.
```jsx
```
#### Piercing into nested properties
If you want to reach into nested attributes (for instance: `mesh.rotation.x`), just use dash-case.
```jsx
```
#### Putting already existing objects into the scene-graph
You can use the `primitive` placeholder for that. You can still give it properties or attach nodes to it. Never add the same object multiples times, this is not allowed in Threejs!
```jsx
const mesh = useMemo(() => new THREE.Mesh(), [])
return
```
#### Using 3rd-party objects declaratively
The `extend` function extends three-fibers catalogue of JSX elements.
```jsx
import { extend } from 'react-three-fiber'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { TransformControls } from 'three/examples/jsm/controls/TransformControls'
extend({ OrbitControls, TransformControls })
// ...
return (
<>
```
# Automatic disposal
Freeing resources is a [manual chore in Threejs](https://threejs.org/docs/#manual/en/introduction/How-to-dispose-of-objects), but React is aware of object-lifecycles, hence three-fiber will attempt to free resources for you by calling `object.dispose()`, if present, on all unmounted objects.
If you manage assets by yourself, globally or in a cache, this may _not_ be what you want. You can switch it off by placing `dispose={null}` onto meshes, materials, etc, or even on parent containers like groups, it is now valid for the entire tree.
```jsx
const globalGeometry = new THREE.BoxBufferGeometry()
const globalMaterial = new THREE.MeshBasicMatrial()
function Mesh() {
return (
```
# Events
Threejs objects that implement their own `raycast` method (meshes, lines, etc) can be interacted with by declaring events on them. We support pointer events, clicks and wheel-scroll. Events contain the browser event as well as the Threejs event data (object, point, distance, etc). You need to [polyfill](https://github.com/jquery/PEP) them yourself, if that's a concern.
Additionally there's a special `onUpdate` that is called every time the object gets fresh props, which is good for things like `self => (self.verticesNeedUpdate = true)`.
Also notcice the `onPointerMissed` on the canvas element, which fires on clicks that haven't hit any meshes.
```jsx
console.log('click')}
onWheel={(e) => console.log('wheel spins')}
onPointerUp={(e) => console.log('up')}
onPointerDown={(e) => console.log('down')}
onPointerOver={(e) => console.log('over')}
onPointerOut={(e) => console.log('out')}
onPointerEnter={(e) => console.log('enter')}
onPointerLeave={(e) => console.log('leave')}
onPointerMove={(e) => console.log('move')}
onUpdate={(self) => console.log('props have been updated')}
/>
```
#### Event data
```jsx
({
...DomEvent // All the original event data
...ThreeEvent // All of Three's intersection data
intersections: Intersect[] // All intersections
object: Object3D // The object that was actually hit
eventObject: Object3D // The object that registered the event
unprojectedPoint: Vector3 // Camera-unprojected point
ray: Ray // The ray that was used to strike the object
camera: Camera // The camera that was used in the raycaster
sourceEvent: DomEvent // A reference to the host event
delta: number // Initial-click delta
}) => ...
```
#### Propagation and capturing
```jsx
onPointerDown={e => {
// Only the mesh closest to the camera will be processed
e.stopPropagation()
// You may optionally capture the target
e.target.setPointerCapture(e.pointerId)
}}
onPointerUp={e => {
e.stopPropagation()
// Optionally release capture
e.target.releasePointerCapture(e.pointerId)
}}
```
# Hooks
Hooks can only be used **inside** the Canvas element because they rely on context! You cannot expect something like this to work:
```jsx
function App() {
const { size } = useThree() // This will just crash
return (