Search code examples
reactjsclojurescriptreagent

Reagent :component-did-mount


I'm trying to set the initial focus on an input element

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

This doesn't work for me though. What am I doing wrong?


Solution

  • As sbensu says with-meta only seems to work in reagent on a function. This means it can be used with identity to produce a reusable wrapper as hoped

    (def initial-focus-wrapper 
      (with-meta identity
        {:component-did-mount #(.focus (reagent/dom-node %))}))
    
    (defn chat-input []
      (fn []
        [initial-focus-wrapper
          [:input {:type "text"}]]))