我需要創建一個包含選擇字段的表單,其中的選項取決於當前可用的郵件帳戶。在頁面加載後,我會在API請求中檢索這些郵件帳戶。爲什麼試劑不能在狀態變化時重新呈現我的試劑形態
以下函數使用select元素創建窗體。
(defn create-options [mail-accounts]
(for [m mail-accounts]
[:option {:key (keyword (str (:id m)))
:value (:id m)}
(:name m)]))
(defn render-mail-account-select [mail-accounts]
(let [form-state (r/atom {})]
(fn [mail-accounts]
(let [form [:form.mailing-form.form-horizontal
(into [:select.form-control {:field :list :id :mail-account}]
(create-options mail-accounts))]]
(pprint form)
[bind-fields form form-state]))))
的pprint
給我下面的輸出:
;; Before the mail-accounts are loaded
[:select.form-control {:field :list, :id :mail-account}]
;; After the state update containing the mail accounts
[:select.form-control
{:field :list, :id :mail-account}
[:option {:key :24, :value 24} "First mail account name"]
[:option {:key :25, :value 25} "Second mail account name"]]
我的問題是,我的網頁上選擇保持爲空,就好像選擇沒有得到重新呈現。
附錄
我想我忘記添加一些代碼: 我在KIOO組件包裹這種形式我取我state
原子在哪裏。然後
(defsnippet choose-account-panel "html/static-panel.html" [:div.panel]
[]
{[:h4.panel-title] (content "3. Mail Account wählen")
[:div.panel-body] (content [render-mail-account-select (:mail-accounts @state)])})
該組件調用我render-mail-account-select
功能,應該正確地重新呈現形式。
(聲明:我不知道我在說什麼)你作出關閉了在咱們結合新試劑原子,但任何事情怎麼能爲了告訴表單更新更新原子?根據我的理解,您需要關閉外部定義的原子,以便可以傳播更改? – zimablue
通過關閉'form-state'原子,我可以創建組件本地狀態。 – Kungi