Does bs-useFetch exist, do we need it, and what it should be like?

reasonreact

#1

Does something like useFetch exist in Reason (haven’t found it on redex)? And if not,

  1. Do you think it’s needed?
  2. How would you type its result? The most obvious is Pending | Data('d) | Error('e), but maybe a combination of Result and something else would be more composable? Or am I overthinking it?
    (Also, how do you encode refetches? Your new request is pending but you already have some stale-ish data. Pending | Data('d) | Refetching ('d) | Error('e), maybe?)

#2

I think https://github.com/bloodyowl/reason-react-update probably has a superset of useFetch's functionality.


#3

There are quite a few ideas out there already that might be good inspiration that I will link below. I think useFetch is a good idea, just like any other hook implementation that makes it easier for people to get started using reason-react without having to think about what promise/future implementation they want to use.


#4

Not sure what you mean. I guess useFetch can be implemented using useReducerWithMapState and, say, bs-fetch, but the package per se doesn’t have Fetch built in, and, anyway, the API feels much more involved than necessary. I think useFetch should be simpler to work with when it’s sufficient.

That said, I very much like the TEA-like approach of reason-react-update :+1:

Yes! Those types look just like what’s needed to me personally.

I guess we should also think about initial JSON parsing, but maybe JSON decoding is better done via Result.flatMap or something :thinking: But then I’m a noob, so feel free to correct me, anyone.

Thank you both for the links!


#5

Decoding would be nice but I am unsure of how that would work. Only with something like decco, since it does codegen, could that be achieved but even then I am not sure. You will probably have to leave that up to the consumer.


#6

Sorry, I worded my previous reply badly. That’s exactly what I meant: expose Js.Json.t and let consumers handle the decoding.