fix: handle Symbol values in hook dependency size change warning#35898
Open
emmaeng700 wants to merge 1 commit intofacebook:mainfrom
Open
fix: handle Symbol values in hook dependency size change warning#35898emmaeng700 wants to merge 1 commit intofacebook:mainfrom
emmaeng700 wants to merge 1 commit intofacebook:mainfrom
Conversation
When a hook's dependency array changes size and contains Symbol values, the error message generation crashes with "Cannot convert a Symbol value to a string" because Array.join() implicitly calls toString() on each element. Use String() explicitly to safely convert any dep value. Fixes facebook#19848
9c1c076 to
4435fd1
Compare
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.
Summary
Fixes a crash where React's dev-mode warning about hook dependency array size changes would itself throw a
TypeError: Cannot convert a Symbol value to a stringwhen any dep was a Symbol.Root cause:
Array.prototype.join()implicitly calls.toString()on each element, which throws for Symbol values. The warning code inareHookInputsEqualused.join()directly:Fix: Use
String()(which handles Symbols correctly) before joining:Fixed in both
ReactFiberHooks.js(client reconciler) andReactFizzHooks.js(server/Fizz renderer) since both had the same issue.Closes #19848
Test plan
warns about variable number of dependencies when deps contain SymbolstoReactHooks-test.internal.jsthat:Symbol(testSymbol)instead of crashingwarns about variable number of dependenciescontinues to pass unchanged