Skip to content

Conversation

@sergiodxa
Copy link
Contributor

@sergiodxa sergiodxa commented Jan 27, 2020

  • If no data call trigger against the key
  • If data is a function call it passing current cached value
  • Return data or throw error after function
  • SWR returns bound mutate function with the key already defined

Resolves #155
Resolves #253

- If no data call trigger against the key
- If data is a function call it passing current cached value
- Return data or throw error after function
@sergiodxa sergiodxa marked this pull request as ready for review January 27, 2020 20:14
@jameslouiz
Copy link

jameslouiz commented Feb 8, 2020

This is exactly what I'm looking for. Great work!

In the mean time, for a bounded Mutate with ability to provide a function with access to the old data you can do this:

const useSWRWithBoundMutate = (key, fetcher, options) => {
  const { data, error } = useSWR(key, fetcher, options);
  const boundMutate = fn => mutate(key, fn(data));
  return { data, error, boundMutate };
};

@omerman
Copy link

omerman commented Feb 12, 2020

This is exactly what I'm looking for. Great work!

In the mean time, for a bounded Mutate with ability to provide a function with access to the old data you can do this:

const useSWRWithBoundMutate = (key, fetcher, options) => {
  const { data, error } = useSWR(key, fetcher, options);
  const boundMutate = fn => mutate(key, fn(data));
  return { data, error, boundMutate };
};

Also what I'm looking for :)!
And it also gives you freedom of accessing the mutate outside react's scope( useSWR hook), because in many cases you would want to update just a part of a given response, and it is impossible to perform it outside react's scope with the current api.
#253

Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks amazing!

And I really like the boundMutate idea. I think we can deprecate trigger (and maybe revalidate as well) in the future.

BTW, can you also add this new usage to the README?

@shuding
Copy link
Member

shuding commented Feb 14, 2020

Also as I commented in #262, I think we should remove the shouldRevalidate trick in https://github.com/zeit/swr/blob/edcfd265d3ad22901b203ecdb25a1fdc92254bf2/src/use-swr.ts#L112-L116 to avoid misunderstanding.

@sergiodxa
Copy link
Contributor Author

@QuietShu I updated the README and removed the shouldRevalidate, always default to true now. Also updated the Optimistic UI example.

@sergiodxa sergiodxa requested a review from shuding February 23, 2020 03:48
@shuding
Copy link
Member

shuding commented Feb 24, 2020

Thank you @sergiodxa!

Close #262, #136.

@shuding shuding merged commit 9160ae5 into vercel:master Feb 24, 2020
@shuding shuding deleted the better-mutate branch February 24, 2020 08:24
akx added a commit to akx/swr that referenced this pull request Mar 30, 2020
akx added a commit to akx/swr that referenced this pull request Mar 31, 2020
shuding pushed a commit that referenced this pull request Mar 31, 2020
* Annotate functions with argument names

* Document bound mutate

Refs #245

* Add code example to bound mutate section

Co-authored-by: Jan Paulus <jan.paulus@mercedes-benz.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessing cached data outside hooks. Return data from mutate

5 participants