ConsoleTVs / sswr

🔥 Svelte stale while revalidate (SWR) data fetching strategy
MIT License
234 stars 11 forks source link

Suggestion: rename useSWR to something else #4

Closed kevmodrome closed 3 years ago

kevmodrome commented 3 years ago

Love the idea of swr in Svelte but it would be neat to keep it aligned with how and what things are called in Svelte.

I assume the use moniker is taken from the react ecosystem (hooks)? The use keyword has a meaning in Svelte and it will probably confuse users a bit. I suggest renaming useSWR to something else. Maybe just... swr()?

ConsoleTVs commented 3 years ago

Hello Kevin, thanks a lot for the feedback.

a PR with this change is relatively easy. I would accept it as long as it updates the docs too.

The use convention is because I also use the same underlying package I built for the vue version of it, that uses the use convention as well. I do understand your concerns and I'm more than welcome to accept such change.

At the end of the day, this change is just for the default SWR instance that we ship with. However, I do think most people will end up creating their own instance, thus allowing them to name it however they want:

import { createSWR } from 'sswr'

const swr = createSWR({
  // Configure a global fetcher for all SWR hooks. This
  // can be replaced with anything that returns a promise
  // with the data inside, for example: axios.
  fetcher: (key) => fetch(key).then((res) => res.json()),
})

// Default instance exports.
export const useSWR = swr.useSWR
export const mutate = swr.mutate
export const revalidate = swr.revalidate
export const clear = swr.clear

You can just change the exports to whatever name suits best for your use case :)

For the default exports feel free to commit a PR!

plibither8 commented 3 years ago

Hi @ConsoleTVs. If you're not working on it, I'm willing to submit a PR for this issue. As Kevin mentioned, just swr would be awesome. Hope that's okay?

plibither8 commented 3 years ago

Whoops! Didn't see the comment reactions or else would've done this long ago :P. Thanks @frederikhors :)

ConsoleTVs commented 3 years ago

I am not really in the track for this anymore, however it is relatively easy to make a custom instance (as in acios) and just re-export the methods with the name you want!