gpt4 book ai didi

使用试剂进行 Ajax GET

转载 作者:行者123 更新时间:2023-12-02 20:48:34 25 4
gpt4 key购买 nike

我正在从我的 Reagent 应用程序执行 Ajax GET,以从数据库加载一些内容。

我不完全确定获取此类 ajax 调用结果到我的页面的最佳方法是什么,考虑到如果我将其放入原子中,那么当取消引用原子时,Reagent 会自动重新渲染组件,这意味着我得到了无限的 ajax 调用序列。

对于一些代码,

(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)))))

这个函数基本上创建了一个[:ul [:li "Stuff here"] [:li "And here"]]

我希望将其显示在我的页面上,该页面现在具有以下代码。

(defn standings-page []
(GET "/list-matches"
{:handler render-matches})
@matches)

最佳答案

我认为最好只将数据保存在原子中,并生成 HTML 作为组件逻辑的一部分。

此外,最好在渲染阶段之外触发 AJAX 调用,例如,在组件安装之前,或者作为事件的结果(例如单击按钮)。

像这样:

(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 post 中称为 form-2 .

关于使用试剂进行 Ajax GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com