Coder Social home page Coder Social logo

Comments (9)

pacocoursey avatar pacocoursey commented on May 14, 2024 31

This is really a matter of preference. Neither of the worst case of both approaches:

const [data, , , revalidate] = useSWR()
const { data: newName, revalidate } = useSWR()

feel particularly difficult to use.

An array approach feels more like a "hook". But as we develop the API and potentially add more information to the response object, the array approach may become unmanageable.

I personally don't find it easy to have to remember how many commas to type, or which position in the array a variable is in.

from swr.

jamesb3ll avatar jamesb3ll commented on May 14, 2024 20

I understand this could be a annoying if you want to use revalidate without isValidating or error but I think is more common to need multiple data with different names than reading those other values in isolation.

Exactly, const [users, _, _unnecessary, _variables, revalidate] = useSWR('/users') isn't a good API.

I think this would be a better solution:

const [users, { error, isValidating, revalidate }] = useSWR('/users', fetch)

As you can deconstruct error, isValidating and revalidate as needed.

Although I think the current API isn't bad at all,const { data: users } = useSWR('/users') is very readable...

from swr.

sztadii avatar sztadii commented on May 14, 2024 11

React team implements react hooks in this way, cause always we will need value and its setter and naming will be not important. The problem is coming when we have many optional params like here. So I think your decision was good 👍

from swr.

paul-vd avatar paul-vd commented on May 14, 2024 6

You don’t need to name ignored values, this should work:

const [data, , , revalidate ] = useSWR(URL, fetch)

that looks horrible though and uncomprehensive for someone seeing the code for the first time.

from swr.

pierreburel avatar pierreburel commented on May 14, 2024 3

I like the way some libs like react-i18next do by returning an array with properties. This way you can decide which one to use depending of your usage : const [users] = useSWR() or const { data, revalidate } = useSWR().

See https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L81-L84

from swr.

cryptiklemur avatar cryptiklemur commented on May 14, 2024 1

Not that I'm advocating for it, as I don't care for the pattern, but there's also the possibility of returning both.

from swr.

conradreuter avatar conradreuter commented on May 14, 2024 1

With @apollo/react-hooks I don't bother with destructuring the return value:

cost fooQuery = useQuery(...)

// extract what we need, but be aware that `query.data` might not exist
const foo = query.data?.foo

if (fooQuery.error) {
  return <Error>{...}</Error>
}
if (fooQuery.loading) {
  return <Loader>{...}</Loader>
}
return <h1>Hello, {foo}</h1>

That being said, I would much more prefer an object as the return value.

from swr.

sergiodxa avatar sergiodxa commented on May 14, 2024

You don’t need to name ignored values, this should work:

const [data, , , revalidate ] = useSWR(URL, fetch)

from swr.

pacocoursey avatar pacocoursey commented on May 14, 2024

Closing as I think we've reached a happy conclusion :)

Thanks everyone for the discussion.

from swr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.