Skip to content

Commit b44a33c

Browse files
committed
fix: make query info object stable
1 parent 67394db commit b44a33c

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/react/tests/useQuery.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ describe('useQuery', () => {
3333
await waitFor(() => rendered.getByText('test'))
3434
})
3535

36+
it('should memoize the query info object', async () => {
37+
const queryKey = Math.random()
38+
const states = []
39+
40+
function Page() {
41+
const state = useQuery(queryKey, () => 'test', { enabled: false })
42+
states.push(state)
43+
return null
44+
}
45+
46+
const rendered = render(<Page />)
47+
rendered.rerender(<Page />)
48+
49+
expect(states.length).toBe(2)
50+
expect(states[0]).toBe(states[1])
51+
})
52+
3653
// See https://github.com/tannerlinsley/react-query/issues/137
3754
it('should not override initial data in dependent queries', async () => {
3855
const queryKey1 = Math.random()

src/react/useBaseQuery.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ export function useBaseQuery(queryKey, config = {}) {
4141
instanceRef.current.run()
4242
}, [enabledBool, query])
4343

44-
return {
44+
const queryState = query.state
45+
46+
const queryInfoRef = React.useRef({})
47+
48+
Object.assign(queryInfoRef.current, {
4549
...query,
46-
...query.state,
50+
...queryState,
4751
query,
48-
}
52+
})
53+
54+
return queryInfoRef.current
4955
}

0 commit comments

Comments
 (0)