react-refetch icon indicating copy to clipboard operation
react-refetch copied to clipboard

Use render props instead of HOC-style `connect`?

Open ianstormtaylor opened this issue 6 years ago • 0 comments

First off, thank you for this library, it is awesomely simple!

I've been toying with react-apollo and the new <Query> and <Mutation> components, and they are really nice in terms of making data declarative, allowing you to co-locate data requirements inside your components.

I was thinking it would be really nice is react-refetch was architected similarly. Instead of connect(mapPropsToRequests, Component), it would be cool do to:

<Refetch
  url={`/users/${id}`}
  method="GET"
>
  {(fetch) => (
	return fetch.pending ? (
      <LoadingAnimation />
    ) : fetch.rejected ? (
      <Error error={fetch.reason} />
    ) : (
      <User={fetch.data} />
    )
  )}
</Refetch>

Or even, for non-GET methods, offer a mutation API:

<Refetch
  url={`/users/${id}`}
  method="POST"
>
  {(updateUser, fetch) => (
	return fetch.rejected ? (
      <Error error={fetch.reason} />
    ) : (
      <Button isLoading={fetch.pending} onClick={updateUser}>Save</Button>
    )
  )}
</Refetch>

What do you think?

ianstormtaylor avatar May 24 '18 23:05 ianstormtaylor