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

useApi doesn't provide a loader instance for usage in useLoader #34

Open
ElForastero opened this issue Aug 28, 2023 · 4 comments
Open

Comments

@ElForastero
Copy link
Contributor

ElForastero commented Aug 28, 2023

* const { loader, trigger } = useApi(createUser);

Currently useApi doesn't provide a reference to the loader, but rather all it's properties.

return { ...loader, trigger, action };

Should we fix this exporting the loader instead of spreading it? It will be a breaking change.

I would rather rewrite docs a little bit to point out that the loader must be first obtained using useLoader(action) hook.

@neurosnap
Copy link
Member

Hi! The intent of these hooks was to mimic something like react-query which returns the loader information + data key in a single object.

Having said that, I'm not a fan of this approach and would be happy to discuss alternative solutions -- including the hooks themselves.

Further, the docs are very barebones and are in dire need of a refresh. Happy to take suggestions.

@ElForastero
Copy link
Contributor Author

By the way, there's another topic related to the loader's state: error responses.

An obstacle I encountered during my experiments with the library is that there's no way to access API error details from the component.

To be more specific, let's consider form validation error responses.

Yes, I can manage the error inside my effect handler, but generally, I'm not fond of the idea of putting too much UI state into the Redux store.

image

I notice that you already have some relevant experience in developing using saga-query. How do you handle such cases? Do you simply put the validation into the store?

@neurosnap
Copy link
Member

There's a bunch of ways you can manage errors, but ultimately error messages and any associated meta data gets stored inside the loader (inside redux).

If your API returns a common structure for API errors, you can a) create a custom middleware to attach the error data to setLoaderError or b) use the errorFn inside loadingMonitor:

errorFn: (ctx: Ctx) => string = (ctx) => ctx.json?.data?.message || '',

requestMonitor also supports passing errorFn:

errorFn?: (ctx: Ctx) => string,

Unfortunately this function doesn't allow users to provide meta information, but I can support that if it's something you're interested in.

@ElForastero
Copy link
Contributor Author

I somehow missed it. Thanks a lot 👍

image

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