noctcore-three/no-resource-creation-in-render
noctcore-three/no-resource-creation-in-render
- Package
- three v0.1.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
Geometries, materials, textures, render targets and loaders are not built in a component body.
A React component body runs on every render. A geometry, material, texture or render target
constructed there is a new GPU resource each time: the previous one is dropped without dispose(),
so GPU memory climbs with each re-render, and the new one is uploaded again. A loader built there
starts with an empty cache every time. Build the resource once:
import * as THREE from 'three';
export function Box({ color }: { color: string }) { const material = new THREE.MeshStandardMaterial({ color }); return <mesh material={material} />;}import { useMemo } from 'react';import * as THREE from 'three';
export function Box({ color }: { color: string }) { const material = useMemo(() => new THREE.MeshStandardMaterial({ color }), [color]); return <mesh material={material} />;}What it flags
Section titled “What it flags”new X() directly in the body of a component, where X is a *Geometry, *Material, *Texture,
*RenderTarget or *Loader class imported from three, or a loader imported from
three/addons, three/examples/jsm or three-stdlib.
A component is a PascalCase function (declared, assigned to a const, a default export, or
wrapped in memo / forwardRef) that contains JSX or calls a hook.
import { useState } from 'react';import { TextureLoader } from 'three';import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
export function Model() { const gltfLoader = new GLTFLoader(); const textureLoader = new TextureLoader(); const [model] = useState(null); return null;}What it does not flag
Section titled “What it does not flag”- Files that import neither
three(or athree/*subpath),three-stdlib, an@react-three/*package nor one ofextraModules: the rule only runs in three.js code, so another engine’s API with the same names (a Phaser or Babylon camera) is never read as three.js. Only staticimportdeclarations count: a file that loads three.js only throughimport()orrequire()is skipped, which can hide a report but never adds one. - Construction inside any nested function:
useMemoanduseEffectcallbacks, lazyuseStateinitialisers, event handlers. - React’s lazy ref idiom:
if (ref.current === null) ref.current = new THREE.BoxGeometry()(alsoif (typeof ref.current === 'undefined')),ref.current ??= new THREE.BoxGeometry(),ref.current = ref.current ?? new THREE.BoxGeometry(), or the same assignment after an early return that fires whenever the ref is set (if (ref.current) return;, also when thatifsits in a bare block above the assignment). A ref the same render empties again after filling it (ref.current = nullas a plain statement after the assignment) is reported, because the next render fills it again; a ref cleared under a branch (if (!on) { ref.current = null; return null; }) or in an effect cleanup is not. The ref is compared as a whole name, and the guard’s polarity is checked; a fallback that is not stored back (props.geometry ?? new THREE.BoxGeometry()) is still reported. - A resource passed straight to a hook (
useRef(new THREE.BoxGeometry())): the extra instances are thrown away after the first render without ever reaching the GPU. - Math objects (
Vector3,Color), which hold no GPU memory. - A PascalCase function with no JSX and no hook call, such as a vanilla three.js factory
function Planet() { return new Mesh(...) }.
import { useLoader } from '@react-three/fiber';import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
export function Model() { const gltf = useLoader(GLTFLoader, '/model.glb'); return <primitive object={gltf.scene} />;}Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
allowIn |
string[] (globs) |
[] |
Files the rule skips, matched against the absolute path and the path relative to the working directory or workspace root. |
extraModules |
string[] |
[] |
More module specifiers that mark a file as three.js code (a project module that re-exports three.js, such as @acme/engine); subpaths match too, and their imports count as three.js classes and R3F hooks. |
'noctcore-three/no-resource-creation-in-render': ['error', { allowIn: ['**/*.stories.tsx'] }]When not to use it
Section titled “When not to use it”A component that is guaranteed to render once (a static Storybook story) leaks nothing in
practice; put those files in allowIn.
Related
Section titled “Related”require-dispose: resources built in an effect, which need their owndispose().no-allocation-in-frame-loop: the same resources built every frame.