Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: (react) explicitly overwrite error variable in state (#3860)

authored by

Arthur Joppart and committed by
GitHub
887cb2a1 917ac600

+48
+5
.changeset/bright-jobs-shout.md
··· 1 + --- 2 + 'urql': patch 3 + --- 4 + 5 + don't persist error state of old gql responses after refetch in react state
+42
packages/react-urql/src/hooks/state.test.ts
··· 1 + import { it, expect, describe } from 'vitest'; 2 + import { computeNextState, initialState } from './state'; 3 + 4 + describe('computeNextState', () => { 5 + it('clears error when new result does not include an error key', () => { 6 + const prevState = { ...initialState, error: new Error('old error') }; 7 + const result = { fetching: false }; 8 + const newState = computeNextState(prevState, result); 9 + expect(newState.data).toBeUndefined(); 10 + expect(newState.error).toBeUndefined(); 11 + expect(newState.fetching).toBe(false); 12 + }); 13 + 14 + it('preserves error when new result is still fetching', () => { 15 + const error = new Error('old error'); 16 + const prevState = { ...initialState, error }; 17 + const result = { fetching: true }; 18 + const newState = computeNextState(prevState, result); 19 + expect(newState.data).toBeUndefined(); 20 + expect(newState.error).toBe(error); 21 + expect(newState.fetching).toBe(true); 22 + }); 23 + 24 + it('sets error when new result has an error', () => { 25 + const error = new Error('something went wrong'); 26 + const result = { fetching: false, error }; 27 + const newState = computeNextState(initialState, result as any); 28 + expect(newState.data).toBeUndefined(); 29 + expect(newState.error).toBe(error); 30 + expect(newState.fetching).toBe(false); 31 + }); 32 + 33 + it('preserves data when result has no data and no error', () => { 34 + const data = { foo: 1 }; 35 + const prevState = { ...initialState, data }; 36 + const result = { fetching: false }; 37 + const newState = computeNextState(prevState, result); 38 + expect(newState.data).toBe(data); 39 + expect(newState.error).toBeUndefined(); 40 + expect(newState.fetching).toBe(false); 41 + }); 42 + });
+1
packages/react-urql/src/hooks/state.ts
··· 55 55 result.data !== undefined || result.error ? result.data : prevState.data, 56 56 fetching: !!result.fetching, 57 57 stale: !!result.stale, 58 + error: result.fetching ? prevState.error : result.error, 58 59 }; 59 60 60 61 return isShallowDifferent(prevState, newState) ? newState : prevState;