Function use_state

Source
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({
    clones!(mut state);

    move || {
      state.set(|mut state| {
        state.greet = "Welcome!";
        state
      });
    }
  }, Deps::some(( /* … */ )));

  let vnode = h!(div).build(state.value().greet);
  vnode
}