Skip to content
Esc

noctcore-three/no-resource-creation-in-render

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:

Incorrect · src/Box.tsx
import * as THREE from 'three';
export function Box({ color }: { color: string }) {
const material = new THREE.MeshStandardMaterial({ color });
return <mesh material={material} />;
}
Correct · src/Box.tsx
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} />;
}

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.

Incorrect · src/Model.tsx · 2 reports
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;
}
  • Files that import neither three (or a three/* subpath), three-stdlib, an @react-three/* package nor one of extraModules: 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 static import declarations count: a file that loads three.js only through import() or require() is skipped, which can hide a report but never adds one.
  • Construction inside any nested function: useMemo and useEffect callbacks, lazy useState initialisers, event handlers.
  • React’s lazy ref idiom: if (ref.current === null) ref.current = new THREE.BoxGeometry() (also if (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 that if sits in a bare block above the assignment). A ref the same render empties again after filling it (ref.current = null as 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(...) }.
Correct · src/Model.tsx
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} />;
}
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'] }]

A component that is guaranteed to render once (a static Storybook story) leaks nothing in practice; put those files in allowIn.