mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 14:54:16 +00:00
docs: expand on the need for prop:value
(#1526)
This commit is contained in:
parent
8ec3fb95f0
commit
3ed49381e3
1 changed files with 29 additions and 1 deletions
|
@ -19,7 +19,8 @@ There are two important things to remember:
|
|||
2. The `value` _attribute_ only sets the initial value of the input, i.e., it
|
||||
only updates the input up to the point that you begin typing. The `value`
|
||||
_property_ continues updating the input after that. You usually want to set
|
||||
`prop:value` for this reason.
|
||||
`prop:value` for this reason. (The same is true for `checked` and `prop:checked`
|
||||
on an `<input type="checkbox">`.)
|
||||
|
||||
```rust
|
||||
let (name, set_name) = create_signal(cx, "Controlled".to_string());
|
||||
|
@ -42,6 +43,33 @@ view! { cx,
|
|||
}
|
||||
```
|
||||
|
||||
> #### Why do you need `prop:value`?
|
||||
>
|
||||
> Web browsers are the most ubiquitous and stable platform for rendering graphical user interfaces in existence. They have also maintained an incredible backwards compatibility over their three decades of existence. Inevitably, this means there are some quirks.
|
||||
>
|
||||
> One odd quirk is that there is a distinction between HTML attributes and DOM element properties, i.e., between something called an “attribute” which is parsed from HTML and can be set on a DOM element with `.setAttribute()`, and something called a “property” which is a field of the JavaScript class representation of that parsed HTML element.
|
||||
>
|
||||
> In the case of an `<input value=...>`, setting the `value` *attribute* is defined as setting the initial value for the input, and setting `value` *property* sets its current value. It maybe easiest to understand this by opening `about:blank` and running the following JavaScript in the browser console, line by line:
|
||||
>
|
||||
> ```js
|
||||
> // create an input and append it to the DOM
|
||||
> const el = document.createElement("input")
|
||||
> document.body.appendChild(el)
|
||||
>
|
||||
> el.setAttribute("value", "test") // updates the input
|
||||
> el.setAttribute("value", "another test") // updates the input again
|
||||
>
|
||||
> // now go and type into the input: delete some characters, etc.
|
||||
>
|
||||
> el.setAttribute("value", "one more time?")
|
||||
> // nothing should have changed. setting the "initial value" does nothing now
|
||||
>
|
||||
> // however...
|
||||
> el.value = "But this works"
|
||||
> ```
|
||||
>
|
||||
> Many other frontend frameworks conflate attributes and properties, or create a special case for inputs that sets the value correctly. Maybe Leptos should do this too; but for now, I prefer giving users the maximum amount of control over whether they’re setting an attribute or a property, and doing my best to educate people about the actual underlying browser behavior rather than obscuring it.
|
||||
|
||||
## Uncontrolled Inputs
|
||||
|
||||
In an "uncontrolled input," the browser controls the state of the input element.
|
||||
|
|
Loading…
Reference in a new issue