diff --git a/.changeset/yellow-shrimps-admire.md b/.changeset/yellow-shrimps-admire.md new file mode 100644 index 0000000000..f3314fda85 --- /dev/null +++ b/.changeset/yellow-shrimps-admire.md @@ -0,0 +1,5 @@ +--- +'@urql/preact': patch +--- + +Apply shallow difference patch from React bindings to `@urql/preact` (See: #3195) diff --git a/packages/preact-urql/src/hooks/useSource.ts b/packages/preact-urql/src/hooks/useSource.ts index bce19f18fb..c256b9cd70 100644 --- a/packages/preact-urql/src/hooks/useSource.ts +++ b/packages/preact-urql/src/hooks/useSource.ts @@ -8,10 +8,26 @@ type Updater = (input: T) => void; let currentInit = false; +// Two operations are considered equal if they have the same key +const areOperationsEqual = ( + a: { key: number } | undefined, + b: { key: number } | undefined +) => { + return a === b || !!(a && b && a.key === b.key); +}; + 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; + for (const key in b) { + if ( + key === 'operation' + ? !areOperationsEqual(a[key], b[key]) + : a[key] !== b[key] + ) { + return true; + } + } return false; };