Fork me on GitHub
#re-frame
<
2023-03-17
>
Joseph Graham07:03:30

Is there any example project that shows how to use re-frame to fetch some stuff using ajax and display it? Re-frame has loads of docs but finding it very hard to imagine how to actually use it.

Ferdinand Beyer10:03:20

You probably want to use https://github.com/day8/re-frame-http-fx. Look at the README for a usage example. You basically define an event for your Ajax call, use the http-xhrio effect to describe how to do the Ajax call and what events to dispatch :on-success and :on-failure, then define those events to handle the response. In your :on-success, update the db as you want, and create subscriptions with reg-sub to extract the data you want to render. This is no longer ajax-specific.

hifumi12307:03:19

Look into the sections of the tutorial on how to deal with side effects.[1] Alternatively, you can jump straight into the docs of re-frame-http-fx[2] [1] https://day8.github.io/re-frame/Talking-To-Servers/ [2] https://github.com/day8/re-frame-http-fx#quick-start-guide

weiss20:03:24

Hi, I am new to the re-frame. I would like to know if is it fine to use component-local ratom in re-frame? Because when I use the following code (which adapted from TODOMVC)

clojure
(defn text-input
  []
  (let [inner (r/atom "")]
    (fn [] [:input
            {:type "text",
             :value @inner,
             :on-change #(reset! inner (-> %
                                           .-target
                                           .-value))}])))
I always get the warning Subscribe was called outside of a reactive context.

hifumi12320:03:01

there are no rf/subscribe calls in that function. you'll need to post code of the actual function calling rf/subscribe

weiss20:03:07

Oh sorry, I thought this warning will occur everytime an atom is dereferenced. It turned out that the reason is something about re-pressed (which is enabled by re-frame template).