Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 561
Brendanator Avatar asked Dec 22 '14 12:12

Brendanator


People also ask

What is component did mount?

The componentDidMount() method allows us to execute the React code when the component is already placed in the DOM (Document Object Model). This method is called during the Mounting phase of the React Life-cycle i.e after the component is rendered.

What is reagent component?

Reagent is a ClojureScript wrapper around React. It gives you a way to easily create React components. Reagent has three main features that make it easy to use: using functions to create React components, using Hiccup to generate HTML, and storing state in Reagent Atoms.

How do you unmount a component in React?

ReactJS componentWillUnmount() Method The componentWillUnmount() method allows us to execute the React code when the component gets destroyed or unmounted from the DOM (Document Object Model). This method is called during the Unmounting phase of the React Life-cycle i.e before the component gets unmounted.

How do I use component did update?

Let's define the parameters used in the componentDidUpdate function: prevProps: This is the first argument, and it passes the previous props to the component. prevState: This is the second argument, and it passes the previous state of the component. snapshot: Returns value using the getSnapshotBeforeUpdate() method.

Should I Update component?

What does “shouldComponentUpdate” do and why is it important ? The shouldComponentUpdate is a lifecycle method in React. This method makes the component to re-render only when there is a change in state or props of a component and that change will affect the output.


1 Answers

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"}]]))
like image 134
Brendanator Avatar answered Oct 05 '22 07:10

Brendanator