solid-ui-react icon indicating copy to clipboard operation
solid-ui-react copied to clipboard

Expose `useSWR` return values in `useDataset` and `useThing`

Open m14t opened this issue 3 years ago • 4 comments

New feature description

This PR exposes two additional fields, mutate and isValidating, from useSWR to the consumers of useDataset and useThing.

It also exposes the dataset field to consumers of useThing.

Context

As they are, useDataset and useThing are useful abstractions for retrieving data, but currently are bit lacking when wanting to then update, or force a refresh on those items. useSWR provides this functionality via the bound (mutate) function it returns, but this currently isn't being exposed.

Checklist

  • [x] All acceptance criteria are met.
  • [x] Relevant documentation, if any, has been written/updated.
  • [x] The changelog has been updated, if applicable.
  • [x] New functions/types have been exported in index.ts, if applicable.
  • [x] New modules (i.e. new .ts files) are listed in the exports field in package.json, if applicable.
  • [x] New modules (i.e. new .ts files) are listed in the typedocOptions.entryPoints field in tsconfig.json, if applicable.
  • [x] Commits in this PR are minimal and have descriptive commit messages.

m14t avatar Oct 13 '22 23:10 m14t

Hey @m14t, thanks for the PR, but I'm not sure if we're able to merge this easily, unfortunately. I'll raise it for discussion, within the Inrupt team, but the fact we use SWR is currently meant to be an implementation detail & not an actual API.

ThisIsMissEm avatar Oct 13 '22 23:10 ThisIsMissEm

Thanks for the quick reply @ThisIsMissEm !

I'd be very open to discussing the api interface you would like to see implemented here and doing the work to bring this inline with that.

To provide some additional background, on a current project we'd like to use these hooks, but need this "mutate" type behavior exposed to get the effect that we are looking for.

An example of a common use-case is when fetching a Subject/Thing that has many Objects under the same Predicate. Each of those Objects have their own component that have a "Remove" button. When the user clicks that button we make a call to the user's pod to remove that entry, but need a way to inform the parent component to refetch the dataset so the UI is updated. With this mutate function exposed we can pass it down to that child component as a onRemove prop that is called at the appropriate time.

The mutate api is also very nice because it also allows for optimistic updates, which we are also using, but would not be strictly required.

m14t avatar Oct 13 '22 23:10 m14t

Hi @m14t, ah, sorry, I was unclear, we need to discuss internally at Inrupt about this, as we've been discussing this SDK in a broader sense. So, this won't be a quick merge, sorry!

ThisIsMissEm avatar Oct 14 '22 11:10 ThisIsMissEm

@m14t as an aside, did you know that useSWR tracks when you use its API? so like, if you do just const { data } = useSWR(..) then it'll only re-render once, even though there was a loading state in there. It's achieved by some kinda fancy getter magic: https://github.com/vercel/swr/blob/main/core/use-swr.ts#L612-L630

ThisIsMissEm avatar Oct 14 '22 11:10 ThisIsMissEm