pub fn use_state<T: 'static>(init: impl FnOnce() -> T) -> State<T>
Expand description

Persist stateful data of the component.

Unlike the use_ref() hook, updating the state will automatically trigger a rerender of the component.

Unlike its React counterpart, setting the state will mutate the underlying data immediately.

Example

fn render(&self) -> VNode {
  let state = use_state(|| State { greet: "Hello!" });

  use_effect({
    let mut state = state.clone();

    move || {
      state.set(|mut state| {
        state.greet = "Welcome!";
        state
      });

      || ()
    }
  }, Deps::some(( /* … */ )));

  h!(div).build(c![state.value().greet])
}