Controlled input loses focus when re-rendered #2350
Replies: 1 comment 4 replies
-
This is not the right way to do things. Take a look at this, instead: #[component]
pub fn Input() -> impl IntoView {
let (name, set_name) = create_signal(Some("Controlled".to_string()));
view! {
<input
type="text"
on:input=move |ev| {
set_name(Some(event_target_value(&ev)));
}
prop:value=move || name().unwrap_or_default()
/>
<hr/>
<pre>{move || format!("{:?}", name())}</pre>
<button on:click=move |_| set_name(None)>"Clear"</button>
}
} React and Leptos work very differently. With React's virtual DOM, you always pay the medium-sized cost of virtual DOM reconciliation. Leptos works directly with actual DOM nodes: there is no diffing overhead beyond what it tells you to do. But what you are telling it to do in your example is,
i.e., by putting a reactive closure around the entire thing, you are causing the entire thing to rerender -- not a React virtual-DOM "render," but an actual DOM rendering, which is why it loses focus -- it is actually a different DOM element. Because there is no virtual DOM, you never pay the cost of a virtual DOM. But you also need to make sure that you make push the actual reactive changes to the "leaves" of the view tree. Note that in the rewritten version, I went for |
Beta Was this translation helpful? Give feedback.
-
One annoying thing with Leptos that doesn't happen in React is that controlled inputs lose focus when re-rendered. This is my code:
Every time I type something, the input loses focus and I have to click on it again to type another key. Here are some solutions I found
<Show />
Option<String>
is_some
instead of using match directly onOption<String>
Option<String>
Is this a limitation of Leptos? Does anyone have ideas on ways that components can have control flow using
match
without re-rendering, or not lose focus when an input is rerendered?Beta Was this translation helpful? Give feedback.
All reactions