Skip to content

Commit 52f8d06

Browse files
committed
Merge branch 'dev'
2 parents b80b2fb + e49fd51 commit 52f8d06

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/components.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
2762
A component has to be [registered](./runtime.md#registering-components) with the runtime before it can be used in the template.
2863

2964
## Template syntax

0 commit comments

Comments
 (0)