Skip to content

Commit

Permalink
add result.error clause
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Feb 2, 2022
1 parent 25d78a9 commit afc1fc3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/react-urql/src/hooks/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ export const initialState = {
error: undefined,
data: undefined,
extensions: undefined,
operation: undefined,
operation: undefined
};

const isShallowDifferent = (a: any, b: any) => {
if (typeof a != 'object' || typeof b != 'object') return a !== b;
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;
return false;
};

interface Stateish {
data?: any;
error?: any;
fetching: boolean;
stale: boolean;
}
Expand All @@ -27,9 +28,10 @@ export const computeNextState = <T extends Stateish>(
const newState = {
...prevState,
...result,
data: result.data !== undefined || result.error ? result.data : prevState.data,
data:
result.data !== undefined || result.error ? result.data : prevState.data,
fetching: !!result.fetching,
stale: !!result.stale,
stale: !!result.stale
};

return isShallowDifferent(prevState, newState) ? newState : prevState;
Expand Down

0 comments on commit afc1fc3

Please sign in to comment.