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?
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.
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.
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.
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.
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.
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"}]]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With