Skip to content

Commit

Permalink
fix: make query info object stable
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jul 15, 2020
1 parent 67394db commit b44a33c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/react/tests/useQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ describe('useQuery', () => {
await waitFor(() => rendered.getByText('test'))
})

it('should memoize the query info object', async () => {
const queryKey = Math.random()
const states = []

function Page() {
const state = useQuery(queryKey, () => 'test', { enabled: false })
states.push(state)
return null
}

const rendered = render(<Page />)
rendered.rerender(<Page />)

expect(states.length).toBe(2)
expect(states[0]).toBe(states[1])
})

// See https://github.com/tannerlinsley/react-query/issues/137
it('should not override initial data in dependent queries', async () => {
const queryKey1 = Math.random()
Expand Down
12 changes: 9 additions & 3 deletions src/react/useBaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ export function useBaseQuery(queryKey, config = {}) {
instanceRef.current.run()
}, [enabledBool, query])

return {
const queryState = query.state

const queryInfoRef = React.useRef({})

Object.assign(queryInfoRef.current, {
...query,
...query.state,
...queryState,
query,
}
})

return queryInfoRef.current
}

0 comments on commit b44a33c

Please sign in to comment.