File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,41 @@ impl Component for MyComponent {
2424}
2525```
2626
27+ ## Example
28+
29+ An example registering a component and its state with the runtime.
30+
31+ ``` rust
32+ #[derive(Debug , Default , State )]
33+ struct MyState {
34+ value : Value <usize >
35+ }
36+
37+ #[derive(Debug , Default )]
38+ struct MyComponent ;
39+
40+ impl Component for MyComponent {
41+ type State = MyState ;
42+ type Message = ();
43+
44+ fn on_key (
45+ & mut self ,
46+ key : KeyEvent ,
47+ state : & mut Self :: State ,
48+ mut children : Children <'_ , '_ >,
49+ mut context : Context <'_ , '_ , Self :: State >,
50+ ) {
51+ * state . value. to_mut () += 1 ;
52+ }
53+
54+ }
55+
56+ let comp = MyComponent ;
57+ let state = MyState { value : 0. into () };
58+
59+ builder . component (" index" , " templates/index.aml" , comp , state ). unwrap ();
60+ ```
61+
2762A component has to be [ registered] ( ./runtime.md#registering-components ) with the runtime before it can be used in the template.
2863
2964## Template syntax
You can’t perform that action at this time.
0 commit comments