question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[useFetch] Feautre request: use refetch also for POST data

See original GitHub issue

Currently useFetch can resend request if I change url. But it would be nice to have the same function if POST data is also changed.

So it can be possible to do something like that:

function useSearch() {
  const query = ref('') // Connect to text input
  const debouncedQuery = ref('')

  // Add delay to not call API while user is typing
  debouncedWatch(query, (query: string) => (debouncedQuery.value = query), { debounce: 500 })

  const { data } = useFetch('https://example.com/search', {
    immediate: false,
    refetch: true,
  })
    .post({ q: query}) // or reactive({ q: query})
    .json()

  return { query, data }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wheatjscommented, Jul 16, 2021

Going to close this out in favor of using something like @isgj suggested

1reaction
isgjcommented, Jun 11, 2021

You can call post in debounceWatch like

function useSearch() {
  const query = ref('') // Connect to text input

  const { data, post } = useFetch('https://example.com/search', {
    immediate: false,
    // refetch: true,
  })
    // .post({ q: query}) // not needed because immediate is false 
    .json()

  // Add delay to not call API while user is typing
  // consider aborting the previous call if one is ongoing before making another call
  debouncedWatch(query, (query: string) => post({ q: query }), { debounce: 500 })

  return { query, data }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

When To Use Refetch Queries in Apollo Client
In this post, I'll explain when it makes sense to use refetchQueries , and when you should rely on Apollo Client's automatic cache ......
Read more >
How to refetch properly an API request after an event occurred?
Just move the api call to a function and call it in the useEffect, onclick or form submit useEffect( () => { apiCall()...
Read more >
Fetching and Updating Data with React Query
We will use the useMutation() hook, which returns an isLoading, error, and mutate function that will be used to make requests and enfold...
Read more >
Refreshing Server-Side Props - Next.js - Josh W Comeau
js to re-fetch the data, on demand, without doing a hard refresh of the whole page? In this short tutorial, I'll share the...
Read more >
2.3 HTTP Post Request with fetch() - YouTube
https://github.com/CodingTrain/Intro-to- Data -APIs-JS We are now in Module #2! In the previous module, we focused on client-side JavaScript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found