我试图将初始焦点设置在输入元素上
(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"}]]))
尽管如此,这对我并没有起作用。我做错了什么?
我试图将初始焦点设置在输入元素上
(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"}]]))
尽管如此,这对我并没有起作用。我做错了什么?
正如sbensu所说,with-meta
似乎只能在reagent函数中使用。这意味着它可以与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"}]]))
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"}]]))
将焦点设置在特定组件上的另一种方法是使用“:auto-focus true”属性:
(defn chat-input []
[:input {:type "text" :auto-focus true}]])
autoFocus
属性设置为true
,那应该可以工作。 - Brigand