dioxus/packages/web/examples/basic.rs

90 lines
2.2 KiB
Rust
Raw Normal View History

2021-07-30 21:04:04 +00:00
//! Basic example that renders a simple VNode to the browser.
use dioxus_core as dioxus;
use dioxus_core::prelude::*;
2021-09-25 01:46:23 +00:00
use dioxus_core_macro::*;
2021-07-30 21:04:04 +00:00
use dioxus_hooks::*;
use dioxus_html as dioxus_elements;
fn main() {
// Setup logging
2021-09-24 04:05:56 +00:00
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
2021-07-30 21:04:04 +00:00
console_error_panic_hook::set_once();
// Run the app
2021-08-24 19:12:20 +00:00
dioxus_web::launch(APP, |c| c)
2021-07-30 21:04:04 +00:00
}
2021-09-25 00:47:59 +00:00
static APP: FC<()> = |cx, _| {
let mut count = use_state(cx, || 3);
2021-09-25 00:47:59 +00:00
let content = use_state(cx, || String::from("h1"));
let text_content = use_state(cx, || String::from("Hello, world!"));
2021-07-30 21:04:04 +00:00
cx.render(rsx! {
2021-09-22 07:22:15 +00:00
div {
2021-09-24 04:05:56 +00:00
h1 { "content val is {content}" }
input {
2021-09-24 04:05:56 +00:00
r#type: "text",
value: "{text_content}"
oninput: move |e| text_content.set(e.value.clone())
2021-09-22 07:22:15 +00:00
}
2021-09-25 00:47:59 +00:00
{(0..10).map(|_| {
2021-09-24 04:05:56 +00:00
rsx!(
button {
onclick: move |_| count += 1,
"Click to add."
"Current count: {count}"
}
br {}
)
})}
2021-09-22 07:22:15 +00:00
select {
name: "cars"
id: "cars"
2021-09-24 04:05:56 +00:00
value: "{content}"
2021-09-22 07:22:15 +00:00
oninput: move |ev| {
content.set(ev.value.clone());
match ev.value.as_str() {
2021-09-22 07:22:15 +00:00
"h1" => count.set(0),
"h2" => count.set(5),
"h3" => count.set(10),
2021-09-24 04:05:56 +00:00
_ => {}
2021-09-22 07:22:15 +00:00
}
},
2021-09-22 07:22:15 +00:00
option { value: "h1", "h1" }
option { value: "h2", "h2" }
option { value: "h3", "h3" }
}
2021-09-24 04:05:56 +00:00
{render_list(cx, *count)}
{render_bullets(cx)}
2021-09-25 00:47:59 +00:00
CHILD {}
2021-07-30 21:04:04 +00:00
}
})
};
2021-08-24 19:12:20 +00:00
fn render_bullets(cx: Context) -> DomTree {
rsx!(cx, div {
"bite me"
2021-09-22 07:22:15 +00:00
})
}
2021-09-24 04:05:56 +00:00
fn render_list(cx: Context, count: usize) -> DomTree {
let items = (0..count).map(|f| {
rsx! {
li { "a - {f}" }
li { "b - {f}" }
li { "c - {f}" }
}
});
rsx!(cx, ul { {items} })
}
2021-09-25 00:47:59 +00:00
static CHILD: FC<()> = |cx, _| rsx!(cx, div {"hello child"});