Question: how to refetch queries passing variables? #442
-
| I have two queries and I would like to update them both to perform a search with the same value from another component: // components/activeUsers.jsx
const { status, data } = useQuery(['users', 'active'], (x, y, searchTerm) => {
  return fetch('/api/users/active?q=' + searchTerm)
}, {
  refetchInterval: 0
})// components/inactiveUsers.jsx
const { status, data } = useQuery(['users', 'inactive'], (x, y, searchTerm) => {
  return fetch('/api/users/inactive?q=' + searchTerm)
}, {
  refetchInterval: 0
})// components/search.jsx
import React, { useState } from 'react'
import { queryCache } from 'react-query'
export default () => {
  const [searchTerm, setSearchTerm] = useState('')
  
  const change = event => {
    const { value } = event.target
    setSearchTerm(value)
    // I KNOW THIS DOESN'T WORK. Its just to communicate what I wish to accomplish
    queryCache.refetchQueries('users', value)
  }
  return (
    <input type="search" value={searchTerm} onChange={change} />
  )
} | 
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 28 replies
-
| Here's an example of how it can be implemented. You'll probably need to debounce your onChange event. See #293 Discussions is a better place for further questions. | 
Beta Was this translation helpful? Give feedback.
-
| I think  | 
Beta Was this translation helpful? Give feedback.
-
| @tannerlinsley any updates on this? It would be very helpful to refetch with different variables. @denisborovikov sandbox no longer works | 
Beta Was this translation helpful? Give feedback.
-
| I am using this to download files, I am having a state called documentId, and when I click on the download button I set the documentId and this will refetch the query and onSuccess I set documentId back to null. This works for me @changyeamoon . Is this a bad practice @denisborovikov ? | 
Beta Was this translation helpful? Give feedback.
-
| I have selector with few params. When user select param i need refetch query in another component. How i can do it? exm: sandbox; | 
Beta Was this translation helpful? Give feedback.
-
| I use the React Hook Form for my forms which works with uncontrolled components. Therefore, the current values are not reflected in useState variables. On form submission, I retrieve the current form values and would like to pass it as variables to the React Query's refetch function. Is there any plan to introduce this feature any time soon? | 
Beta Was this translation helpful? Give feedback.
-
| 
 // Api call
async function disabledConfig(configId?: string) {
  if (typeof configId === 'undefined') {
    return
  }
  // If you have id, make you API call here ...
  // ...
}
// I create a custom hook for react query. 
// I set enable = false here for making a manual fetch via `refetch`. And by using ref, instead of primitive value,
// I will get the updated value for making API call
function useDisabledConfig(configIdRef: React.MutableRefObject<string | undefined>) {
  return useQuery('disable-config', () => disabledConfig(configIdRef.current), {
    enabled: false
  })
}
// In Config component
function Config(props: {configId: string, onDisabled: (id: string) => void}) {
  const { configId, onDisabled } = props
  function handleDisabled() {
    onDisabled(configId)
  }
  return <button onClick={handleDisabled}>Disabled</button>
}
function ListConfig() {
  // For example, you get list config from somewhere: API response or props
  const listConfig = ...
 
  // This is the ref I use for handle disabled
 const configIdRef = useRef<string | undefined>()
  // Make API call with custom hook
  const {refetch: disableConfig} = useDisabledConfig(configIdRef)
 function handleDisable(configId: string) {
  // Update the ref to the latest id value
  configIdRef.current = configId
  // Make manual API call
  // When this function is call, the custom hook by using the ref will get the lates configId
  disableConfig()
 }
  return <>
    {
      listConfig.map(config => <Config key={config.id configId={config.id} onDisabled={handleDisable} />) 
    }
    </>
} | 
Beta Was this translation helpful? Give feedback.
-
| I have a slightly different use case than I see above. I have Redis setup to cache queries server side. Sometimes I may to want fetch results ignoring the cached Redis data, but normally I just pull from the Redis cache if it's available. If choosing to skip the cache I was hoping to use  Alternatively I guess I could have a separate  const {
  isFetching,
  data,
  error,
  refetch,
} = useQuery(
  ["user", user], 
  (skipCache = false) => fetch(`/api/${user}?skipCache=${skipCache}`).then(res => res.json())
);
// On button click refresh and skip Redis cache
refetch(true); | 
Beta Was this translation helpful? Give feedback.
-
| 
 Those are two different things imo - clearing the cache is not the same as bypassing the cache. If you want to clear the cache, it's a side-effect and I think that you should use a mutation for that. But it doesn't sound like  Anyhow, I don't see a good solution to do this with just queries. What you can do is just have a mutation and have that mutation return a result that you then put into the query cache. See Updates from Mutation Responses. | 
Beta Was this translation helpful? Give feedback.
-
| Hey guys, I am sorry I did not realize that this feature is really wanted by everyone! I am the maintainer of  It required a lot bunch of edits that impacted nearly all files, my work is far from being done (I did some  Now, I don't know what to do with the work I've done since this is a proprietary codebase and my ideas are likely to be fought! But I will be open to discussion and ideas to make this feature "working" and working good! | 
Beta Was this translation helpful? Give feedback.
-
| Wouldn't  | 
Beta Was this translation helpful? Give feedback.

Here's an example of how it can be implemented.
You'll probably need to debounce your onChange event. See #293
Discussions is a better place for further questions.