-
Notifications
You must be signed in to change notification settings - Fork 45.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn if MutableSource snapshot is a function #18933
Warn if MutableSource snapshot is a function #18933
Conversation
if (typeof snapshot === 'function') { | ||
console.warn( | ||
'Mutable source should not return a functions as the snapshot value. ' + | ||
'Functions may close over mutable values and cause tearing.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message probably isn't great. Happy to change it.
6f52afb
to
d45cd7d
Compare
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 6f52afb:
|
Details of bundled changes.Comparing: 142d4f1...2c45e61 react-dom
react-art
react-test-renderer
react-reconciler
react-native-renderer
Size changes (experimental) |
Details of bundled changes.Comparing: 142d4f1...2c45e61 react-native-renderer
react-dom
react-art
react-test-renderer
react-reconciler
ReactDOM: size: 0.0%, gzip: 0.0% Size changes (stable) |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 2c45e61:
|
const snapshot = getSnapshot(source._source); | ||
if (__DEV__) { | ||
if (typeof snapshot === 'function') { | ||
console.warn( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be console.error? Seems very error like if it might give wrong output and we might make it an invariant.
useMutableSource does not properly support snapshots that are functions. In part this is because of how it is implemented internally (the function gets mistaken for a state updater function). To fix this we could just wrap another function around the returned snapshot, but this pattern seems problematic to begin with- because the function that gets returned might itself close over mutable values, which would defeat the purpose of using the hook in the first place. This PR proposes adding a new DEV warning if the snapshot returned is a function. It does not change the behavior (meaning that a function could still work in some cases- but at least the current behavior prevents passing around a closure that may later become stale unless you're really intentional about it e.g. () => () => {...}).
d45cd7d
to
2c45e61
Compare
Resolves #18823; potential alternative of #18921.
useMutableSource
does not properly support snapshots that are functions. In part this is because of how it is implemented internally (the function gets mistaken for a state updater function). To "fix" this we could just wrap another function around the returned snapshot, but this pattern seems problematic to begin with- because the function that gets returned might itself close over mutable values, which would defeat the purpose of using the hook in the first place.This PR proposes adding a new DEV warning if the snapshot returned is a function. It does not change the behavior (meaning that a function could still "work" in some cases- but at least the current behavior prevents passing around a closure that may later become stale unless you're really intentional about it e.g.
() => () => {...}
).Open to discussion on whether we should lock it down further and treat this as an invariant.