easy-peasy icon indicating copy to clipboard operation
easy-peasy copied to clipboard

Call external function from store

Open Hatsunyan opened this issue 6 years ago • 2 comments

Hello. I have some question. I need to call some error handler after my thunk get error from api. I think save function to store is bad idea, but pass error handler with props with every call is uncomfortably. Is there a way to call some external function from model?

Hatsunyan avatar Jul 31 '20 12:07 Hatsunyan

Not sure if this is best practice, but I've made a ApiResponse object based on what error my backend returns, as I have to handle each error differently dependent on what type of error that is returned.

My service function returns an ApiResponse-object contains an enum for return success/error and the object interfaced to what my thunk/model wants to handle

If the object matches my interface I handle it in the model or let my AuditModel handle the error. It might be 'overworking' the handling, but it makes it easy for me to handle errors and make sure my data is correct

EagDk avatar Aug 05 '20 09:08 EagDk

@Hatsunyan you could make use of the store injections to do this?

https://easy-peasy.now.sh/docs/api/thunk.html#dependency-injection

Hope that helps!

ctrlplusb avatar Sep 04 '20 08:09 ctrlplusb