ClojureScript Reagent 组件不断刷新导致许多 POST 次调用
ClojureScript Reagent Component Constantly Refreshed Resulting in Many POST Calls
一段时间以来,我一直在尝试修复此错误,但我还是无能为力。问题与 for 循环有关,因为当我删除它时,对组件的调用仅限于一次,但它会一直被调用。这导致我想消除的无限 POST 调用。简而言之,组件会不断呈现,而不仅仅是在加载页面时呈现。
(defn get-messages []
"Gets the messages from the server"
(let [response (r/atom "")]
(fn []
(POST "/get" {:handler #(reset! response %)})
[:div
(for [item @response]
[:div
[:h3.you (first item)]
[:p (second item)]])])))
我把它称为任何其他组件:
(defn test []
[:div
[get-messages]])
post得到的数据就是
(["Bill" "What is the weather today?"] ["Jim" "The weather is warm"])
编辑
我意识到我遇到的错误与惰性序列无关。很抱歉不清楚,但错误是渲染其中包含 POST 的对象。 AJAX 不断被调用。为了修复它,我包括:
(:require-macros [cljs.core.async.macros :as cam])
[clojure.core.async :as ac]
然后我在POST周围使用了这个:
(cam/go
(<! (ac/timeout 500))
(POST "/ajax/get-message" {:handler #(reset! response %)}))
感谢大家的耐心等待。
干杯,
马特
根据所提供的上下文,有两件事需要解决:
for
returns 一个惰性序列。你可能希望结果是一个向量,所以试试这个:
(POST "/get" {:handler #(reset! response %)})
(into [:div]
(vec (for [item @response]
[:div
[:h3 (first item)]
[:p (second item)]])))
....
这将产生以下结构,这就是您想要的(从上面的代码打印):
[:div
[:div [:h3 "Bill"] [:p "What is the weather today?"]]
[:div [:h3 "Jim"] [:p "The weather is warm"]]]
我不确定这是否是问题所在,但这是良好的开端。
其次 - 您的间距在很多地方都不正确,这可能会导致涉及不匹配的括号的细微错误,这可能会导致循环不按您预期的方式运行。在你的最后一行,]]]]
应该是 ]])]
,因此 for
没有被关闭,例如。作为其他示例,您在 POST
行之后缩进了 [:div
,但它不应该缩进,并且您对所有缩进都使用了一个 space,除了嵌套之外,它应该是两个向量。
一段时间以来,我一直在尝试修复此错误,但我还是无能为力。问题与 for 循环有关,因为当我删除它时,对组件的调用仅限于一次,但它会一直被调用。这导致我想消除的无限 POST 调用。简而言之,组件会不断呈现,而不仅仅是在加载页面时呈现。
(defn get-messages []
"Gets the messages from the server"
(let [response (r/atom "")]
(fn []
(POST "/get" {:handler #(reset! response %)})
[:div
(for [item @response]
[:div
[:h3.you (first item)]
[:p (second item)]])])))
我把它称为任何其他组件:
(defn test []
[:div
[get-messages]])
post得到的数据就是
(["Bill" "What is the weather today?"] ["Jim" "The weather is warm"])
编辑
我意识到我遇到的错误与惰性序列无关。很抱歉不清楚,但错误是渲染其中包含 POST 的对象。 AJAX 不断被调用。为了修复它,我包括:
(:require-macros [cljs.core.async.macros :as cam])
[clojure.core.async :as ac]
然后我在POST周围使用了这个:
(cam/go
(<! (ac/timeout 500))
(POST "/ajax/get-message" {:handler #(reset! response %)}))
感谢大家的耐心等待。
干杯, 马特
根据所提供的上下文,有两件事需要解决:
for
returns 一个惰性序列。你可能希望结果是一个向量,所以试试这个:
(POST "/get" {:handler #(reset! response %)})
(into [:div]
(vec (for [item @response]
[:div
[:h3 (first item)]
[:p (second item)]])))
....
这将产生以下结构,这就是您想要的(从上面的代码打印):
[:div
[:div [:h3 "Bill"] [:p "What is the weather today?"]]
[:div [:h3 "Jim"] [:p "The weather is warm"]]]
我不确定这是否是问题所在,但这是良好的开端。
其次 - 您的间距在很多地方都不正确,这可能会导致涉及不匹配的括号的细微错误,这可能会导致循环不按您预期的方式运行。在你的最后一行,]]]]
应该是 ]])]
,因此 for
没有被关闭,例如。作为其他示例,您在 POST
行之后缩进了 [:div
,但它不应该缩进,并且您对所有缩进都使用了一个 space,除了嵌套之外,它应该是两个向量。