首页 > 解决方案 > 手动观看重帧订阅

问题描述

给定一个具有值的重新构建数据库:score

(def my-db {:score 0})

(re-frame.core/reg-sub ::score (fn [db] (:score db)))

(re-frame.core/reg-event-db
 ::initialize-db
 (fn [_ _] my-db))

(re-frame.core/reg-event-db
 ::set-score
 (fn [db [_ score]]
   (assoc db :score score)))

我希望我应该能够订阅 ::score 并添加一个观察者

> (def score-atom (re-frame.core/subscribe [::score]))

> (add-watch
   score-atom
   :whatever
   (fn [_ _ _ _] (prn "here!")))

然后我应该能够触发更新:

> (re-frame.core/dispatch [::set-score 2])
nil
> @score-atom
"here!"
2
>

与正常的 atom 行为相反,在完成外部 deref 之前,更新不会到达手表。

显然我可以用试剂应用程序包裹手表,但感觉很重。如何让手表立即触发?我宁愿不要在每次发送调度时手动取消订阅。

标签: clojurescriptreagentre-frame

解决方案


如果我跑步,事情似乎会有所进展(reagent.core/flush)


推荐阅读