Once you’ve grokked the Re-Frame philosophy for building truly reactive applications on React in ClojureScript, you will want to apply it everywhere. One obvious and ubiquitous application of effects is asynchronous HTTP requests, and of course there is an officially sanctioned library for handling these. One problem: it relies on an AJAX library that only works with the browser XHR implementation. So what can we do in React Native-based Clojurescript apps?
It’s actually very simple, and can be accomplished in about ten lines of code.
We’ll rely on cljs-http, a
core.async
-based HTTP library.
The opts
param here is a map that will handle everything that cljs-http
takes as input, like :headers
, or :multipart-params
. That documentation
can be found in the cljs-http
README.
Here’s an example of how you might use it:
You’ll want to have registered the :save-user
and :alert-failure
events
before using them, of course.
Hopefully this small event function has been helpful. It’s short enough that I
don’t think it rises to the level of being a library instead of a blog post, and
in fact I think most people will want to customize which keys they want to
handle. I personally have replaced opts
with just edn-params
and headers
as those are all I use. Feel free to reach out
over twitter or in #cljsrn
on
Clojurians Slack with questions.