--- title: 'use_state' --- `use_state` is used to manage state in a function component. It returns a `UseStateHandle` object which `Deref`s to the currently stored value and provides a `set` method to update the value. The hook takes a function as input which determines the initial value. :::tip Advanced tip The setter function is guaranteed to be the same across the entire component lifecycle. You can safely omit the `UseStateHandle` from the dependents of `use_effect_with_deps` if you only intend to set values from within the hook. ::: This hook will always trigger a re-render upon receiving a new state. See [`use_state_eq`](#use_state_eq) if you want the component to only re-render when the state changes. ## Example ```rust use std::ops::Deref; use yew::{Callback, function_component, html, use_state, Html}; #[function_component] fn StateExample() -> Html { let name_handle = use_state(|| String::from("Bob")); let name = name_handle.deref().clone(); let onclick = { let name = name.clone(); Callback::from(move |_| name_handle.set(format!("{}y Jr.", name))) }; html! {
{ "My name is: " } { name }