试剂:组件安装

Bre*_*tor 18 clojurescript reactjs reagent

我正在尝试将初始焦点设置在输入元素上

(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"}]]))
Run Code Online (Sandbox Code Playgroud)

这不适合我.我究竟做错了什么?

Bre*_*tor 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"}]]))
Run Code Online (Sandbox Code Playgroud)


sbe*_*nsu 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))))}))
Run Code Online (Sandbox Code Playgroud)

所以你的代码应该是:

(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"}]]))
Run Code Online (Sandbox Code Playgroud)