-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
useSubscription: fix rules of React violations #11863
Merged
+370
−356
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
987ff6d
Revert "Merge pull request #9707 from kazekyo/fix_usesubscription_in_…
phryneas 03d89ea
essentially rewrite useSubscription
phryneas 06db150
use `setResult` update method
phryneas 71dc7aa
adjust tests
phryneas ac0d804
change observable during render; lazy initialization of subscription
phryneas 4aa14cf
no more need for stable options, performance optimization
phryneas af260cf
changeset
phryneas e249bad
adjust documentation
phryneas 297919f
review feedback
phryneas 9f64d3f
Apply suggestions from code review
phryneas 654d949
clarify comment
phryneas 74fe836
Merge remote-tracking branch 'origin/release-3.11' into pr/rules-of-h…
phryneas 1af05a6
update size-limits
phryneas 33829ec
fix test
phryneas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading status checks…
no more need for stable options, performance optimization
commit 4aa14cfe643356802f01b897130f3cd589c57e3a
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"dist/apollo-client.min.cjs": 39585, | ||
"dist/apollo-client.min.cjs": 39573, | ||
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32821 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,6 @@ import { useApolloClient } from "./useApolloClient.js"; | |
import { useDeepMemo } from "./internal/useDeepMemo.js"; | ||
import { useSyncExternalStore } from "./useSyncExternalStore.js"; | ||
|
||
const emptyObject = Object.freeze({}); | ||
|
||
/** | ||
* > Refer to the [Subscriptions](https://www.apollographql.com/docs/react/data/subscriptions/) section for a more in-depth overview of `useSubscription`. | ||
* | ||
|
@@ -114,10 +112,7 @@ export function useSubscription< | |
TVariables extends OperationVariables = OperationVariables, | ||
>( | ||
subscription: DocumentNode | TypedDocumentNode<TData, TVariables>, | ||
options: SubscriptionHookOptions< | ||
NoInfer<TData>, | ||
NoInfer<TVariables> | ||
> = emptyObject | ||
options: SubscriptionHookOptions<NoInfer<TData>, NoInfer<TVariables>> = {} | ||
) { | ||
const hasIssuedDeprecationWarningRef = React.useRef(false); | ||
const client = useApolloClient(options.client); | ||
|
@@ -145,9 +140,6 @@ export function useSubscription< | |
|
||
let { skip, fetchPolicy, variables, shouldResubscribe, context } = options; | ||
variables = useDeepMemo(() => variables, [variables]); | ||
if (typeof shouldResubscribe === "function") { | ||
shouldResubscribe = !!shouldResubscribe(options!); | ||
} | ||
|
||
let [observable, setObservable] = React.useState(() => | ||
options.skip ? null : ( | ||
|
@@ -161,11 +153,13 @@ export function useSubscription< | |
} | ||
} else if ( | ||
!observable || | ||
(shouldResubscribe !== false && | ||
(client !== observable.__.client || | ||
subscription !== observable.__.query || | ||
!equal(variables, observable.__.variables) || | ||
fetchPolicy !== observable.__.fetchPolicy)) | ||
((client !== observable.__.client || | ||
subscription !== observable.__.query || | ||
fetchPolicy !== observable.__.fetchPolicy || | ||
!equal(variables, observable.__.variables)) && | ||
phryneas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(typeof shouldResubscribe === "function" ? | ||
!!shouldResubscribe(options!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
: shouldResubscribe) !== false) | ||
) { | ||
setObservable( | ||
(observable = createSubscription( | ||
|
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.
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.
I believe we've been using this version of an empty object throughout the project.
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.
Fair enough.