/
main.rs
76 lines (64 loc) · 2.16 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use gloo::console;
use js_sys::Date;
use yew::{html, Component, Context, Html};
// Define the possible messages which can be sent to the component
pub enum Msg {
Increment,
Decrement,
}
pub struct App {
value: i64, // This will store the counter value
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
Self { value: 0 }
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Increment => {
self.value += 1;
console::log!("plus one"); // Will output a string to the browser console
true // Return true to cause the displayed change to update
}
Msg::Decrement => {
self.value -= 1;
console::log!("minus one");
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<div>
<div class="panel">
// A button to send the Increment message
<button class="button" onclick={ctx.link().callback(|_| Msg::Increment)}>
{ "+1" }
</button>
// A button to send the Decrement message
<button onclick={ctx.link().callback(|_| Msg::Decrement)}>
{ "-1" }
</button>
// A button to send two Increment messages
<button onclick={ctx.link().batch_callback(|_| vec![Msg::Increment, Msg::Increment])}>
{ "+1, +1" }
</button>
</div>
// Display the current value of the counter
<p class="counter">
{ self.value }
</p>
// Display the current date and time the page was rendered
<p class="footer">
{ "Rendered: " }
{ String::from(Date::new_0().to_string()) }
</p>
</div>
}
}
}
fn main() {
yew::Renderer::<App>::new().render();
}