Реагент: компонентный монтаж
Я пытаюсь установить начальный фокус на элемент ввода
(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"}]]))
Это не работает для меня. Что я делаю неправильно?
Ответы
Ответ 1
Как говорит 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"}]]))
Ответ 2
Я думаю, что 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"}]]))