【问题标题】:How do I do timed page refresh in Clojurescript with Reagent?如何使用试剂在 Clojurescript 中进行定时页面刷新?
【发布时间】:2018-10-15 15:48:44
【问题描述】:

我正在编写一个 Clojurescript SPA,它需要定期(可能每 30 秒一次,可能每分钟一次)轮询服务器并获取一些更新的数据。

我应该如何在 Clojurescript 中使用 Reagent(React 框架)执行此操作?

我是只使用 Javascript 的低级 setTimeout() 还是在 Clojurescript / React 中有更惯用的方法来做到这一点?

【问题讨论】:

    标签: reactjs clojurescript reagent


    【解决方案1】:

    The Reagent examples 好好表现一下:

    (ns simpleexample.core
      (:require [reagent.core :as r]))
    
    (defonce timer (r/atom (js/Date.)))
    
    (defonce time-color (r/atom "#f34"))
    
    (defonce time-updater (js/setInterval
                           #(reset! timer (js/Date.)) 1000))
    

    第一个例子in the re-frame docs类似:

    ;; -- Domino 1 - Event Dispatch -----------------------------------------------
    
    (defn dispatch-timer-event
      []
      (let [now (js/Date.)]
        (rf/dispatch [:timer now])))  ;; <-- dispatch used
    
    ;; Call the dispatching function every second.
    ;; `defonce` is like `def` but it ensures only one instance is ever
    ;; created in the face of figwheel hot-reloading of this file.
    (defonce do-timer (js/setInterval dispatch-timer-event 1000))
    

    与 Clojure 一样,在许多情况下,我们重用主机平台的现有机器,而不用包装器伪装(或用新代码重新发明它)。

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 2021-07-22
      • 1970-01-01
      相关资源
      最近更新 更多