Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Using the Error Function on Modifers #81

Open
mmcxii opened this issue Jun 17, 2021 · 1 comment
Open

Example Using the Error Function on Modifers #81

mmcxii opened this issue Jun 17, 2021 · 1 comment

Comments

@mmcxii
Copy link
Collaborator

mmcxii commented Jun 17, 2021

None of the current tests/ docs demonstrate the use of the error function on modifiers like useUpdateFactory. It would be beneficial to see a basic implementation.

@k2snowman69
Copy link
Member

Good callout, we have tests for error for the indexable scenarios but not really for the basic store.
https://github.com/snowcoders/react-context-store/blob/main/src/context-store--indexable/modifiers/get-create-one-context-data.test.ts#L401

The error callback is for updating the data in a particular way if an error happens or the promise rejects. It's particularly useful when making optimistic updates to the data store. For example:

  1. User says "I want to POST this data"
  2. Code in the preload updates the store immediately so that the change reflects to the user immediately but is running the API request in the background
  3. API request fails and rejects the promise
  4. The error callback is fired so the data can be reset to it's original value or some other state if you want to handle error states separately.

Really it's only used for optimistic updates, where if you're doing the update only after the API comes back, if the API fails, the store wasn't updated in the first place so there's no clean up required.

I'll leave this open as a reminder to add optimistic update tests in src/context-store--basic/tests so there are examples of this.

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

No branches or pull requests

2 participants