#How to use state as an attribute

1 messages · Page 1 of 1 (latest)

crystal glacier
#

I'm trying to store a variable in state and have it always updated alongside an input field. It looks something like this:

#[function_component]
fn component() -> Html {
  let n = use_state(|_| 5.into());
  html! {
    <input type="number" value={n}></input>
  }
}

I get this error:

error[E0277]: the trait bound `UseStateHandle<_>: IntoPropValue<Option<implicit_clone::unsync::IString>>` is not satisfied
  --> src/main.rs:21:24
   |
21 |                 value={n}
   |                        ^ the trait `IntoPropValue<Option<implicit_clone::unsync::IString>>` is not implemented for `UseStateHandle<_>`
   |

I read this as "you can't use state as an attribute" but certainly I'm just doing something wrong. I also don't see any method available on the UseStateHandle struct that will read/convert the value as needed. Why does an attribute value need to come from props? This component needs to keep track of some global state that will perform some different arithmetic based on n. What am I missing here?

pale wraith
#

just dereference it

pale wraith
#
#[function_component]
fn component() -> Html {
  let n = use_state(|| 5);
  html! {
    <input type="number" value={"".to_owned() + &(*n.to_string())} />
  }
}

#

try as that

crystal glacier
#

I swear I tried that and it didnt work before. I guess was jamming too many cloning methods together and didn't get it. Thank you @pale wraith