Skip to content
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

Fix re-rendering when another component reuses the same query #3195

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fifty-sheep-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'urql': patch
---

Fix re-rendering when another component reuses the same query without a change in the result
nathan-knight marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ There are multiple commands you can run in the root folder to test your changes:

```sh
# TypeScript checks:
pnpnm run check
pnpm run check

# Linting (prettier & eslint):
pnpm run lint
Expand Down
21 changes: 16 additions & 5 deletions packages/react-urql/src/hooks/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ export const initialState = {
operation: undefined,
};

const isShallowDifferent = (a: any, b: any) => {
if (typeof a != 'object' || typeof b != 'object') return a !== b;
for (const x in a) if (!(x in b)) return true;
for (const x in b) if (a[x] !== b[x]) return true;
/**
* Checks if two objects are shallowly different with a special case for
* 'operation' where it compares the key if they are not the otherwise equal
*/
const isShallowDifferent = <T extends Record<string, any>>(a: T, b: T) => {
for (const key in a) if (!(key in b)) return true;
for (const key in b) {
if (a[key] !== b[key]) {
// Two operations are considered equal if they have the same key
if (key === 'operation' && a.operation?.key === b.operation?.key) {
continue;
}
return true;
}
}
kitten marked this conversation as resolved.
Show resolved Hide resolved
return false;
};

Expand All @@ -27,7 +38,7 @@ export const computeNextState = <T extends Stateish>(
prevState: T,
result: Partial<T>
): T => {
const newState = {
const newState: T = {
...prevState,
...result,
data:
Expand Down