clojurescript - OmClojurescript:问题呈现重置应用程序状态

clojurescript - OmClojurescript:问题呈现重置应用程序状态,第1张

我正在尝试使用om显示一个组件,只要此小部件所需的数据到达。我提出了以下(大致):

(def data (atom     {}))                                                               

(go (let [response (<! (http/get "../rest/ds" ))]                               
  (reset! data (:result (:body response)))))

(om/root
  (fn [app owner]                                                                  
    (reify
     om/IInitState
     (init-state [_]
       (prn "(1) returning initial state now")
       {:text "Hello world!"})
     om/IRenderState 
     (render-state [this state]                                                   
        (prn state) ; <-- here: not the state that I reset! before but the original state
        (do-something ...))))                                                  
  data
  {:target (. js/document (getElementById "app"))})

state中的render-state似乎永远不是我用reset!设置的@data,尽管似乎会触发重新渲染。我在这里用错了吗?直接访问render-state中的reset!时,它会显示我使用push()设置的正确值。

最佳答案:

1 个答案:

答案 0 :(得分:1)

(prn state)中的状态是组件状态。原子data是应用程序状态,在您的组件中,您调用了app

(om/root
  (fn [app owner]  ; <- `app` is the application state                                                              
    (reify
     om/IInitState
     (init-state [_]
       (prn "(1) returning initial state now")
       {:text "Hello world!"})
     om/IRenderState 
     (render-state [this state]                                                   
        (prn app) ; <- refer to application state instead
        (do-something ...))))                          
  data
  {:target (. js/document (getElementById "app"))})
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复