如何使试剂中的输入发生变化?

How to make inputs change in Reagent?

我是 React、Clojure、ClojureScript 和 Reagent 的新手,所以我可能遗漏了一些简单的东西。

我正在修改这里的代码 (https://reagent-project.github.io) 和默认的 hello-world ClojureScript 页面以获得一个简单的登录页面。但是每当我在用户名和密码字段中输入内容时,什么也没有发生。这是为什么?

(ns cljs_test.core
    (:require [reagent.core :as reagent :refer [atom]]))

(enable-console-print!)

;; define your app data so that it doesn't get over-written on reload

(defonce app-state (atom {:text "Hello world!"}))

; define Reagent component `atom-input`
(defn atom-input [state]
  [:input {:type "text"
    :style {:display "block"}
    :placeholder "Username"
    :value @state
    :on-change (fn [event]
      reset! state (-> event .-target .-value))}])
; define Reagent component `atom-pw`
(defn atom-pw [state]
  [:input {:type "password"
    :style {:display "block"}
    :placeholder "Password"
    :value @state
    :on-change (fn [event]
      reset! state (-> event .-target .-value))}])

下面我们使用 square-bracket "function call".

调用两个 Reagent 组件 atom-inputatom-pw
(defn loginForm [someUrl]
  (let [unVal (atom "") pwVal (atom "")]
    (fn []
      [:div
        [:p "Username: " @unVal]
        [:p "Password: " @pwVal]

        ; Reagent does "magic" here to transform any vector beginning
        ; with a function into a real function call
        [atom-input unVal]             ; line 32
        [atom-pw pwVal]                ; line 33

        [:button "to-do submit button"]])))

(defn app []
  [:div
    [:div
      [:h1 (:text @app-state)]
      [:h3 "Edit this and watch it change!"]]
    [loginForm ""]])

(reagent/render-component [app]
                          (. js/document (getElementById "app")))

(defn on-js-reload []
  ;; optionally touch your app-state to force rerendering depending on
  ;; your application
  ;; (swap! app-state update-in [:__figwheel_counter] inc)
)

提前感谢您的回复。

缺少括号。应该看起来像:

(defn atom-input [state]
  [:input {:type        "text"
           :style       {:display "block"}
           :placeholder "Username"
           :value       @state
           :on-change   (fn [event]
                          (reset! state (-> event .-target .-value)))}])

Clojure 中的括号进行函数调用。没有它们,reset!state 将被忽略。这个表达式:

(-> event .-target .-value)

将生成一个值,然后由 :on-change 的匿名函数返回该值。但是,此值将被忽略。