Skip to content

refactor(wrapEffect): split out of util.tsx, support ref as a prop - #352

Merged
kvvasuu merged 1 commit into
masterfrom
fix/02-wrapeffect-refactor
Jul 30, 2026
Merged

refactor(wrapEffect): split out of util.tsx, support ref as a prop#352
kvvasuu merged 1 commit into
masterfrom
fix/02-wrapeffect-refactor

Conversation

@kvvasuu

@kvvasuu kvvasuu commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Stacked PR 2/6 — part of a split of #350 .
← based on #351 · → followed by #353

Pulls wrapEffect out of the general util grab-bag into its own module, and lets generated effect components accept ref as a regular prop (React 19) instead of having no ref support at all - previously none of the wrapEffect-based effects (Bloom, Vignette, HueSaturation, etc.) could be given a ref. Autofocus gets the same ref-as-prop treatment directly (it doesn't go through wrapEffect).

Also fixes two related wrapEffect bugs:

  • The args memo fingerprinted props with JSON.stringify, which threw on circular references (textures, refs, and other three.js objects commonly hold one) - two independent reports (fixes cyclic object value error in Scanline when using useRef #333, fixes circular structure to JSON --> starting at object with constructor #334) hit this on ordinary usage. Swapped in a cycle-safe stringify.
  • That fingerprint also called JSON.stringify's implicit toJSON, so THREE.Texture.toJSON() was re-encoding the whole image to a base64 data URL on every render just to compute a value that got discarded
    • measured ~39ms for a 512x512 texture. Fingerprinting now walks own properties directly and collapses typed arrays/buffers to an identity token instead.
  • ref itself wasn't destructured out of props, so it leaked into that same fingerprint; once mounted, ref.current pointed at the effect instance, so every render after the first fingerprinted a different value and silently rebuilt the instance in a loop.

Adds a shared test harness (test-utils.tsx) used by this and later commits' test suites.

@kvvasuu
kvvasuu requested a review from krispya July 29, 2026 18:25
Base automatically changed from fix/01-deps-and-tooling to master July 30, 2026 20:31
Pulls wrapEffect out of the general util grab-bag into its own module,
and lets generated effect components accept ref as a regular prop
(React 19) instead of having no ref support at all - previously none
of the wrapEffect-based effects (Bloom, Vignette, HueSaturation, etc.)
could be given a ref. Autofocus gets the same ref-as-prop treatment
directly (it doesn't go through wrapEffect).

Also fixes two related wrapEffect bugs:
- The args memo fingerprinted props with JSON.stringify, which threw
  on circular references (textures, refs, and other three.js objects
  commonly hold one) - two independent reports (#333, #334) hit this
  on ordinary usage. Swapped in a cycle-safe stringify.
- That fingerprint also called JSON.stringify's implicit toJSON, so
  THREE.Texture.toJSON() was re-encoding the whole image to a base64
  data URL on every render just to compute a value that got discarded
  - measured ~39ms for a 512x512 texture. Fingerprinting now walks own
  properties directly and collapses typed arrays/buffers to an
  identity token instead.
- ref itself wasn't destructured out of props, so it leaked into that
  same fingerprint; once mounted, ref.current pointed at the effect
  instance, so every render after the first fingerprinted a different
  value and silently rebuilt the instance in a loop.

Adds a shared test harness (test-utils.tsx) used by this and later
commits' test suites.
@kvvasuu
kvvasuu force-pushed the fix/02-wrapeffect-refactor branch from 4abfa19 to f798cb4 Compare July 30, 2026 20:31
@krispya

krispya commented Jul 30, 2026

Copy link
Copy Markdown
Member

This works much better than what was there but it reveals a deeper issue. We should not reconstruct the effect whenever a prop is mutated and move to an explicit args prop like R3F.

<Bloom args={[{ mipmapBlur: false }]} intensity={2} />

@kvvasuu
kvvasuu merged commit e018959 into master Jul 30, 2026
1 check passed
@kvvasuu
kvvasuu deleted the fix/02-wrapeffect-refactor branch July 30, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

circular structure to JSON --> starting at object with constructor cyclic object value error in Scanline when using useRef

2 participants