Ajax GET with reagent

I am doing an Ajax GET from my Reagent application to download some things from the database.

I'm not quite sure what the best way to get the result of such an ajax call to my page, given that if I put it in an atom, Reagent will automatically re-render the component when the atom is dereferenced, which means that I get an endless sequence of ajax calls.

For some code

(def matches (atom nil)) (defn render-matches [ms] (reset! matches (into [:ul] (map (fn [m] ^{:key m}[:li m]) (walk/keywordize-keys (t/read (t/reader :json) ms))))) 

This function basically creates [:ul [:li "Stuff here"] [:li "And here"]]

What I would like to display on my page, which now has the following code.

 (defn standings-page [] (GET "/list-matches" {:handler render-matches}) @matches) 
+7
ajax clojurescript reagent
source share
1 answer

I think it’s better to save only the data in the atom and generate HTML as part of the component logic.

It is also better to activate an AJAX call outside the visualization phase, for example, before the component is mounted, or as a result of an event (for example, pressing a button).

Like this:

 (def matches (atom nil)) (defn component [] (let [get-stuff (fn [] (GET "/..." :handler (fn [response] (reset! matches (:body response))))] (get-stuff) <-- called before component mount (fn [] [:ul (for [m match] ^{:key ...} [:li ...])]))) 

This is called form-2 in this post .

+10
source share

All Articles