Skip to content

fix(animated): WeakMap fallback for non-extensible component caching (#2533) - #2657

Open
Techsynx wants to merge 1 commit into
pmndrs:nextfrom
Techsynx:fix/2533-hermes-non-extensible-cache-next
Open

Techsynx wants to merge 1 commit into
pmndrs:nextfrom
Techsynx:fix/2533-hermes-non-extensible-cache-next

Conversation

@Techsynx

Copy link
Copy Markdown

Why

Resolves #2533.

In React Native environments running the Hermes JavaScript engine (or whenever host components / higher-order components are frozen or have their extensions prevented via Object.preventExtensions()), calling animated(Component) throws an uncaught exception:
TypeError: Cannot add property Symbol(AnimatedComponent), object is not extensible

This issue was previously patched on the v10 branch in PR #2535, but was never ported to the default next development branch. As a result, users of the next line still experience crashes when animating React Native host components under Hermes.

What

  • Added a module-level WeakMap<object, any> fallback cache in packages/animated/src/createHost.ts.
  • When caching an animated wrapper, we first attempt to attach it to the component object via Component[cacheKey] = cached.
  • If assigning property cacheKey throws (because the component is non-extensible or frozen), the error is caught and the wrapper is stored in the fallbackCache WeakMap.
  • Subsequent lookups check Component[cacheKey] ?? fallbackCache.get(Component).
  • Added unit tests in packages/animated/src/createHost.test.ts verifying that non-extensible and frozen components do not throw, retain their cached wrapper identity across invocations, and preserve their displayName.
  • Added patch changeset in .changeset/non-extensible-component-cache.md.

Checklist

  • Ready to be merged

@changeset-bot

changeset-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 89b28c4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@react-spring/animated Patch
@react-spring/core Patch
@react-spring/parallax Patch
@react-spring/rafz Patch
@react-spring/shared Patch
@react-spring/types Patch
@react-spring/three Patch
@react-spring/web Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@joshuaellis

Copy link
Copy Markdown
Member

the next branch doesnt have react native so how are you using it?

@Techsynx

Copy link
Copy Markdown
Author

Thanks for checking this @joshuaellis!

You're completely right that @react-spring/native has been removed on next (in favor of Reanimated as noted in the changeset).

The reason for proposing this on next was that @react-spring/animated is still a shared package used across targets (@react-spring/web, @react-spring/three, etc.). In createHost.ts:

Component[cacheKey] = withAnimated(Component, hostConfig)

When someone wraps an arbitrary component with animated(Component), if that component is frozen or non-extensible (such as components wrapped with Object.freeze() or Object.preventExtensions() by certain libraries or utility wrappers), mutating Component[cacheKey] throws a runtime TypeError: Cannot add property Symbol(AnimatedComponent), object is not extensible. The WeakMap fallback makes animated(Component) safe for any frozen or non-extensible React component.

That said, since the original issue (#2533) was specifically reported in the context of React Native on Hermes, and @react-spring/native has been dropped on next, if you feel this defensive fallback isn't needed on next or prefer to close #2533 as resolved on v10 and not applicable to next, please feel free to close this PR!

This branch has not been deployed

No deployments
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.

animated() throws 'cannot add a new property' on non-extensible React Native host components (Hermes)

2 participants