3

我有原子foo

(defonce foo (r/atom "foo"))

我有父组件:

(defn parent-component []
  (js/setTimeout #(reset! foo "bar") 5000)
  (child-component {:foo foo}))

我有子组件:

(defn child-component [props]
  (let [derefed (deref (:foo props))]
    (fn []
      [:div
       [:p derefed]
       [:p (deref (:foo props))]])))

重置后仅更新第二段foo

为什么它会这样工作?

4

1 回答 1

5

来自有关 Form-2 组件的重新框架文档:https ://github.com/Day8/re-frame/wiki/Creating-Reagent-Components#form-2--a-function-returning-a-function 。

您需要在内部函数中再次重复外部函数参数:

(defn child-component [props]
  (fn [props]
    (let [derefed (deref (:foo props))]
      [:div
       [:p derefed]
       [:p (deref (:foo props))]])))
于 2017-09-21T19:36:18.220 回答