[][src]Function seed_hooks::use_state

pub fn use_state<T, F>(data_fn: F) -> StateAccess<T> where
    F: FnOnce() -> T,
    T: 'static, 

Constructs a T accessor. T is stored keyed to the current topological context. The accessor always references this context therefore can you can set/update/ or get this T from anywhere.

The passed closure is only used for the first initialisation of state. Subsequent evaluations of this function just returns the accessor. Only one type per context can be stored in this way.

Examples

let my_string =  use_state(|| "foo".to_string());
...
...
 // Maybe in a Callback...
my_string.set("bar")

This stores a string "foo" in the current topological context, which is later set to "bar", in some other part of the program.

You can store Clone or non-Clone types. Although non-Clone types need to be read via their accessor in a more restrictive way.