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: always render the latest observer state #1449

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/react/tests/useInfiniteQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ describe('useInfiniteQuery', () => {
// Set state
expect(states[4]).toMatchObject({
data: { pages: ['0-desc', '1-desc'] },
isFetching: false,
isFetching: true,
isFetchingNextPage: false,
isSuccess: true,
isPreviousData: false,
isPreviousData: true,
})
expect(states[5]).toMatchObject({
data: { pages: ['0-desc', '1-desc'] },
Expand Down Expand Up @@ -842,7 +842,7 @@ describe('useInfiniteQuery', () => {
// Set state
expect(states[2]).toMatchObject({
hasNextPage: true,
data: { pages: [0] },
data: { pages: [7, 8] },
isFetching: false,
isFetchingNextPage: false,
isSuccess: true,
Expand Down
18 changes: 9 additions & 9 deletions src/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ describe('useQuery', () => {
// First success
expect(states[1]).toMatchObject({ isLoading: false, isSuccess: true })
// Switch
expect(states[2]).toMatchObject({ isLoading: false, isSuccess: true })
expect(states[2]).toMatchObject({ isLoading: true, isSuccess: false })
// Second load
expect(states[3]).toMatchObject({ isLoading: true, isSuccess: false })
// Second success
Expand Down Expand Up @@ -758,7 +758,7 @@ describe('useQuery', () => {
// Fetched
expect(states[1]).toMatchObject({ data: 1 })
// Rerender
expect(states[2]).toMatchObject({ data: 1 })
expect(states[2]).toMatchObject({ data: undefined })
// Switch
expect(states[3]).toMatchObject({ data: undefined })
// Fetched
Expand Down Expand Up @@ -1087,9 +1087,9 @@ describe('useQuery', () => {
// Set state
expect(states[2]).toMatchObject({
data: 0,
isFetching: false,
isFetching: true,
isSuccess: true,
isPreviousData: false,
isPreviousData: true,
})
// Previous data
expect(states[3]).toMatchObject({
Expand Down Expand Up @@ -1155,9 +1155,9 @@ describe('useQuery', () => {
// Set state
expect(states[2]).toMatchObject({
data: 0,
isFetching: false,
isFetching: true,
isSuccess: true,
isPreviousData: false,
isPreviousData: true,
})
// Previous data
expect(states[3]).toMatchObject({
Expand Down Expand Up @@ -1240,7 +1240,7 @@ describe('useQuery', () => {
data: 0,
isFetching: false,
isSuccess: true,
isPreviousData: false,
isPreviousData: true,
})
// Switched query key
expect(states[4]).toMatchObject({
Expand Down Expand Up @@ -1322,7 +1322,7 @@ describe('useQuery', () => {
data: 10,
isFetching: false,
isSuccess: true,
isPreviousData: false,
isPreviousData: true,
})
// Switched query key
expect(states[2]).toMatchObject({
Expand Down Expand Up @@ -2112,7 +2112,7 @@ describe('useQuery', () => {
// Initial
expect(states[0]).toMatchObject({ data: { count: 0 } })
// Set state
expect(states[1]).toMatchObject({ data: { count: 0 } })
expect(states[1]).toMatchObject({ data: { count: 1 } })
// Update
expect(states[2]).toMatchObject({ data: { count: 1 } })
})
Expand Down
7 changes: 3 additions & 4 deletions src/react/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ export function useBaseQuery<TData, TError, TQueryFnData, TQueryData>(
observer.setOptions(defaultedOptions)
}

const [currentResult, setCurrentResult] = React.useState(() =>
observer.getCurrentResult()
)
const [, rerender] = React.useState({})
const currentResult = observer.getCurrentResult()

// Subscribe to the observer
React.useEffect(() => {
errorResetBoundary.clearReset()
return observer.subscribe(notifyManager.batchCalls(setCurrentResult))
return observer.subscribe(notifyManager.batchCalls(rerender))
}, [observer, errorResetBoundary])

// Handle suspense
Expand Down