试剂:组件安装

问题描述 投票:18回答:3

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

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

虽然这对我不起作用。我在做什么错?

reactjs clojurescript reagent
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"}]]))

5
投票

我认为with-meta应该以函数作为参数。从文档:


0
投票

设置给定组件焦点的另一种方法是使用“:auto-focus true”属性:

© www.soinside.com 2019 - 2024. All rights reserved.