18

入力要素に初期フォーカスを設定しようとしています

(defn initial-focus-wrapper [element]
  (with-meta element
    {:component-did-mount #(.focus (reagent/dom-node %))}))

(defn chat-input []
  (fn []
    [initial-focus-wrapper
      [:input {:type "text"}]]))

これは私にはうまくいきません。私は何を間違っていますか?

4

3 に答える 3

14

sbensu が言うwith-metaように、関数の試薬でのみ機能するようです。identityこれは、希望どおりに再利用可能なラッパーを生成するために使用できることを意味します

(def initial-focus-wrapper 
  (with-meta identity
    {:component-did-mount #(.focus (reagent/dom-node %))}))

(defn chat-input []
  (fn []
    [initial-focus-wrapper
      [:input {:type "text"}]]))
于 2014-12-24T12:21:32.417 に答える
5

with-meta関数を引数としてとるべきだと思います。ドキュメントから:

(def my-html (atom ""))

(defn plain-component []
  [:p "My html is " @my-html])

(def component-with-callback
  (with-meta plain-component
    {:component-did-mount
     (fn [this]
       (reset! my-html (.-innerHTML (reagent/dom-node this))))}))

したがって、コードは次のようになります。

(defn initial-focus-wrapper [element]
  (with-meta element
    {:component-did-mount #(.focus (reagent/dom-node %))}))

(defn chat-input []
  [initial-focus-wrapper
    (fn []
      [:input {:type "text"}]]))
于 2014-12-23T18:35:26.600 に答える