-
-
Notifications
You must be signed in to change notification settings - Fork 929
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: useContractRead
& useContractReads
data instability
#659
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'wagmi': patch | ||
--- | ||
|
||
Added an `isDataEqual` config option to `useContractRead`, `useContractReads` & `useContractInfiniteReads` to define whether or not that data has changed. Defaults to `deepEqual`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'wagmi': patch | ||
--- | ||
|
||
Fixed an issue where `useContractRead` & `useContractReads` would return unstable data. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,7 +169,7 @@ describe('multicall', () => { | |
"hex": "0x05a6db", | ||
"type": "BigNumber", | ||
}, | ||
undefined, | ||
null, | ||
] | ||
`) | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { useBaseQuery, useQuery, useInfiniteQuery } from './query' | ||
export { useChainId } from './useChainId' | ||
export { useForceUpdate } from './useForceUpdate' | ||
export { useInvalidateOnBlock } from './useInvalidateOnBlock' | ||
export { useSyncExternalStore } from './useSyncExternalStore' | ||
export { useSyncExternalStoreWithTracked } from './useSyncExternalStoreWithTracked' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as React from 'react' | ||
import { QueryKey, useQueryClient } from 'react-query' | ||
|
||
import { useBlockNumber } from '../network-status' | ||
|
||
export function useInvalidateOnBlock({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think it's useful to have a basic test that checks the query cache between new blocks? Not easy to do with our current testing setup, but maybe at least we should add a |
||
enabled, | ||
queryKey, | ||
}: { | ||
enabled?: boolean | ||
queryKey: QueryKey | ||
}) { | ||
const { data: blockNumber } = useBlockNumber({ | ||
enabled, | ||
watch: enabled, | ||
}) | ||
const queryClient = useQueryClient() | ||
|
||
const startBlock = React.useRef<number>() | ||
React.useEffect(() => { | ||
if (!enabled) return | ||
if (blockNumber === startBlock.current) return | ||
if (!startBlock.current) { | ||
startBlock.current = blockNumber | ||
return | ||
} | ||
|
||
queryClient.invalidateQueries(queryKey) | ||
}, [blockNumber, enabled, queryClient, queryKey]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! Updated, and now the implementation is more basic 👌 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old implementation was refetching on
provider
changes (as well as block number changes), but we are already checking forprovider
changes in our query key (viachainId
), so it was double fetching. We still want to watch on block number, so I have refactored this to "invalidate on block" ifwatch & !cacheOnBlock
is truthy.