fix: return toPrimitive function instead of throwing for React 19 compat#635
Open
deggertsen wants to merge 1 commit intoLegendApp:mainfrom
Open
fix: return toPrimitive function instead of throwing for React 19 compat#635deggertsen wants to merge 1 commit intoLegendApp:mainfrom
deggertsen wants to merge 1 commit intoLegendApp:mainfrom
Conversation
React 19's development mode introduces `logComponentRender` which inspects component props by calling `Symbol.toPrimitive` on them. When Legend State observables are passed as props, this triggers the proxy's `get` trap for `Symbol.toPrimitive`, which previously threw an error. This crashes the entire component tree in React 19 dev mode. The fix changes the behavior from throwing to: - Returning a proper `Symbol.toPrimitive` function (`NaN` for numeric hint, `'[Observable]'` for string/default hint) - Emitting a `console.warn` in development mode to still help developers catch accidental primitive coercion This is a non-breaking change for production code (the throw was only useful as a dev-time guard), and it's consistent with how other Proxy-based state libraries handle this scenario. Fixes React 19 compatibility where `addValueToProperties` → `logComponentRender` in react-dom-client would crash when encountering Legend State observables. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
React 19's development mode introduces a new
logComponentRenderfunction that inspects component props by iterating their properties and callingSymbol.toPrimitiveto convert them to loggable values. When Legend State observables are passed as props (which is a common pattern), this triggers the Proxy'sgettrap forSymbol.toPrimitive, which currently throws:This crashes the entire component tree in React 19 dev mode via the error boundary.
Solution
Change the
Symbol.toPrimitivehandler in the Proxy from throwing to returning a proper toPrimitive function:NaNfor numeric hint (arithmetic operations still produce obviously wrong results)'[Observable]'for string/default hint (string concatenation shows the observable wasn't unwrapped)console.warnin development mode to help developers catch accidental primitive coercionThis preserves the developer experience (you still get a warning when you accidentally use an observable as a primitive) while being compatible with React 19's dev-mode inspection.
Why not keep the throw?
The throw was a dev-time guard to catch
observable + 1mistakes. But:Symbol.toPrimitivefor dev loggingconsole.warnserves the same purpose — developers still see the warning'[Observable]'return values make the bug obvious even without the warning (you'll seeNaNor[Observable]in your UI)Test Changes
Updated the existing "Adding observables should throw" test to verify the new behavior: warns + returns NaN instead of throwing.
Made with Cursor