简体   繁体   中英

Reagent doesn't rerender component with deref-ing inside of let

I have atom foo :

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

I have parent component:

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

And I have child component:

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

Only second paragraph is updated after reseting foo .

Why is it working that way?

From the re-frame documentation regarding Form-2 components: https://github.com/Day8/re-frame/wiki/Creating-Reagent-Components#form-2--a-function-returning-a-function .

You need to repeat the outer function parameters again in the inner function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM