on master help sign in
examples/counter: use the builder for rendering
112586cNick Fitzgerald
examples/counter/src/lib.rs+18−26
@@ -1,5 +1,5 @@
1use dodrio::bumpalo::{self, Bump};
1use dodrio::bumpalo::{self, Bump};
2−use dodrio::{on, Node, Render};
2+use dodrio::Render;
3use log::*;
3use log::*;
4use wasm_bindgen::prelude::*;
4use wasm_bindgen::prelude::*;
5
5
@@ -32,19 +32,15 @@ impl Render for Counter {
32 where
32 where
33 'a: 'bump,
33 'a: 'bump,
34 {
34 {
35+ use dodrio::builder::*;
36+
35 // Stringify the count as a bump-allocated string.
37 // Stringify the count as a bump-allocated string.
36 let count = bumpalo::format!(in bump, "{}", self.count);
38 let count = bumpalo::format!(in bump, "{}", self.count);
37
39
38− Node::element(
39− bump,
40− "div",
40+ div(bump)
41− [],
41+ .children([
42− [],
43− [
44− Node::element(
45− bump,
46− "button",
42+ button(bump)
47− [on(bump, "click", |root, vdom, _event| {
43+ .on("click", |root, vdom, _event| {
48 // Cast the root render component to a `Counter`, since
44 // Cast the root render component to a `Counter`, since
49 // we know that's what it is.
45 // we know that's what it is.
50 let counter = root.unwrap_mut::<Counter>();
46 let counter = root.unwrap_mut::<Counter>();
@@ -55,24 +51,20 @@ impl Render for Counter {
55 // Since the count has updated, we should re-render the
51 // Since the count has updated, we should re-render the
56 // counter on the next animation frame.
52 // counter on the next animation frame.
57 vdom.schedule_render();
53 vdom.schedule_render();
58− })],
54+ })
59− [],
60− [Node::text("+")],
55+ .children([text("+")])
61− ),
56+ .finish(),
62− Node::text(count.into_bump_str()),
57+ text(count.into_bump_str()),
63− Node::element(
64− bump,
65− "button",
58+ button(bump)
66− [on(bump, "click", |root, vdom, _event| {
59+ .on("click", |root, vdom, _event| {
67 // Same as above, but decrementing instead of incrementing.
60 // Same as above, but decrementing instead of incrementing.
68 root.unwrap_mut::<Counter>().decrement();
61 root.unwrap_mut::<Counter>().decrement();
69 vdom.schedule_render();
62 vdom.schedule_render();
70− })],
63+ })
71− [],
72− [Node::text("-")],
64+ .children([text("-")])
73− ),
65+ .finish(),
74− ],
66+ ])
75− )
67+ .finish()
76 }
68 }
77}
69}
78
70