Saya baru mengenal React, Clojure, ClojureScript, dan Reagent, jadi saya mungkin melewatkan sesuatu yang sederhana.

Saya memodifikasi kode dari sini (https://reagent-project.github.io) dan default halaman ClojureScript hello-world untuk mendapatkan halaman login sederhana. Tetapi setiap kali saya mengetik sesuatu di bidang nama pengguna dan kata sandi, tidak ada yang terjadi. Kenapa ini?

(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))}])

Di bawah ini kami memanggil dua komponen Reagen atom-input dan atom-pw menggunakan rel kurung persegi "panggilan fungsi".

(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)
)

Terima kasih sebelumnya atas tanggapannya.

0
Agent 008 19 Maret 2020, 18:39

1 menjawab

Jawaban Terbaik

Tanda kurung hilang. Harus terlihat seperti:

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

Parens di Clojure membuat panggilan fungsi. Tanpa mereka, reset! dan state diabaikan. Ekspresi ini:

(-> event .-target .-value)

Akan menghasilkan nilai, yang kemudian dikembalikan oleh fungsi anonim untuk :on-change. Namun, nilai ini diabaikan.

1
Alan Thompson 19 Maret 2020, 16:15