ava / use-http

🐶 React hook for making isomorphic http requests
https://use-http.com
MIT License
2.32k stars 114 forks source link

Refetch data #217

Closed JanSmolko closed 4 years ago

JanSmolko commented 4 years ago

I have similar question as in #161 and that is: How can I refetch data? (when I dont want to change anything in URL).

I thought that I can use it this way:

const { get, data, loading, error } = useFetch(
    path,
    {
      cache: 'no-cache'
    },
    []
  )

and then in some callback use

get()

but I can call new request only with something like

get("?"+timestamp)

Is there any way to do it without changing URL? Or am I missing something?

alex-cory commented 4 years ago
const { get, data, loading, error } = useFetch(url, {
  path: '/you/path/here',
  cache: 'no-cache'
}, [])

The 1st argument of useFetch if it's a string, will always be the URL or overwrite the global URL. That's why we have the path option.

You can also do this if you have your global url setup in your Provider

const { get, data, loading, error } = useFetch({
  path: '/you/path/here',
  cache: 'no-cache'
}, [])
alex-cory commented 4 years ago

Going to close this, but feel free to comment more if you have any more questions or need more clarifications!

JanSmolko commented 4 years ago

@alex-cory Sorry but I still dont get it. (To me) it seems like you answered to some other issue :/ When I call get() again, It will not make another request.

alex-cory commented 4 years ago

Could you make a codesandbox showing me the problem please? :)

alex-cory commented 4 years ago

Oh, try cachePolicy: 'no-cache'. If you don't want cache anywhere, you can set it in the Provider

JanSmolko commented 4 years ago

Oh, try cachePolicy: 'no-cache'. If you don't want cache anywhere, you can set it in the Provider

https://codesandbox.io/s/stoic-meadow-yu6g9

OMG, thank you. thats it.

BTW: typescript had no problem with cache: 'no-cache' and what is really working is cachePolicy: CachePolicies.NO_CACHE

tuvudu commented 3 years ago

@alex-cory Many Thanks!