comp_state: store state on components

comp_state is a crate that allows you to store state on a per component basis. It is designed as a clone of React Hooks, principally the useState hook.

Here a component is defined as a 'topological aware execution context', this means that the component is aware of its own call-site identity and location in the call tree.

comp_state is generally used within the context of a host framework, for instance a web frontend compiled to Wasm.

Example:

This is a complete counting button with state implemented in in the Seed framework:

```rust use compstate::{topo, usestate};

[topo::nested]

fn hookstylebutton() -> Node { // Declare a new state variable which we'll call "count" let (count, countaccess) = usestate(|| 0); div![ p![format!("You clicked {} times", count)], button![ onclick( move || count_access.set(count + 1)), format!("Click Me × {}", count) ] ] } ```

vs ReactJs:

```javascript import React, { useState } from 'react'; function Example() { // Declare a new state variable, which we'll call "count" const [count, setCount] = useState(0);

return (

You clicked {count} times

); } ```

The two most important functions are:

Caveats:

This is purely alpha experimental!

Each component has its own "topo::Id" which is then used as a key to store component state. topo is a crate from the Moxie team who are creating a GUI framework for rust. There is an interesting talk about moxie and how topo works here.

How does it work?

Why would anyone want to do this?