Skip to content

Commit 755a21b

Browse files
authored
refactor(react-api-client): fix react query key object hashing for useAllCommandsAsPreSerializedList (#15188)
React query does not properly handle objects passed in useQuery's queryKey array argument that contain keys with `undefined` values. Here, in `useAllCommandsAsPreSerializedList`, I map undefined values to null so that they are properly cached and do not trigger a refetch when the host object does not change.
1 parent 87bf426 commit 755a21b

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

react-api-client/src/runs/useAllCommandsAsPreSerializedList.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import mapValues from 'lodash/mapValues'
12
import { UseQueryResult, useQuery } from 'react-query'
23
import { getCommandsAsPreSerializedList } from '@opentrons/api-client'
34
import { useHost } from '../api'
@@ -28,18 +29,10 @@ export function useAllCommandsAsPreSerializedList<TError = Error>(
2829
enabled: host !== null && runId != null && options.enabled !== false,
2930
}
3031
const { cursor, pageLength } = nullCheckedParams
31-
// reduce hostKey into a new object to make nullish values play nicely with react-query key hash
32-
const hostKey =
33-
host != null
34-
? Object.entries(host).reduce<Object>((acc, current) => {
35-
const [key, val] = current
36-
if (val != null) {
37-
return { ...acc, [key]: val }
38-
} else {
39-
return { ...acc, [key]: 'no value' }
40-
}
41-
}, {})
42-
: {}
32+
33+
// map undefined values to null to agree with react query caching
34+
// TODO (nd: 05/15/2024) create sanitizer for react query key objects
35+
const hostKey = mapValues(host, v => (v !== undefined ? v : null))
4336

4437
const query = useQuery<CommandsData, TError>(
4538
[

0 commit comments

Comments
 (0)