dioxus/packages/web/examples/deep.rs

72 lines
1.7 KiB
Rust
Raw Normal View History

2021-03-14 00:11:06 +00:00
use dioxus_web::{dioxus::prelude::*, WebsysRenderer};
2021-03-13 15:02:57 +00:00
fn main() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
console_error_panic_hook::set_once();
2021-03-14 00:11:06 +00:00
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(CustomA))
2021-03-13 15:02:57 +00:00
}
2021-03-14 00:11:06 +00:00
use components::CustomB;
2021-03-13 15:02:57 +00:00
fn CustomA<'a>(ctx: Context<'a>, props: &'a ()) -> DomTree {
2021-03-29 16:31:47 +00:00
let (val, set_val) = use_state(&ctx, || "abcdef".to_string());
2021-03-26 19:50:28 +00:00
2021-03-14 00:11:06 +00:00
ctx.render(rsx! {
2021-03-13 15:02:57 +00:00
div {
2021-03-14 00:11:06 +00:00
class: "m-8"
2021-03-13 15:02:57 +00:00
"CustomA {val}"
button {
"Upper"
2021-03-29 16:31:47 +00:00
onclick: move |_| set_val(val.to_ascii_uppercase())
2021-03-13 15:02:57 +00:00
}
button {
"Lower"
2021-03-29 16:31:47 +00:00
onclick: move |_| set_val(val.to_ascii_lowercase())
2021-03-13 15:02:57 +00:00
}
CustomB {
2021-03-26 19:50:28 +00:00
val: val.as_ref()
2021-03-13 15:02:57 +00:00
}
}
})
}
2021-03-14 00:11:06 +00:00
mod components {
use super::*;
2021-03-13 15:02:57 +00:00
2021-03-14 00:11:06 +00:00
#[derive(Debug, Props, PartialEq)]
pub struct PropsB<'src> {
val: &'src str,
}
pub fn CustomB<'a>(ctx: Context<'a>, props: &'a PropsB<'a>) -> DomTree {
let (first, last) = props.val.split_at(3);
ctx.render(rsx! {
div {
class: "m-8"
"CustomB {props.val}"
CustomC {
val: first
}
CustomC {
val: last
}
2021-03-13 15:02:57 +00:00
}
2021-03-14 00:11:06 +00:00
})
}
2021-03-13 15:02:57 +00:00
2021-03-14 00:11:06 +00:00
#[derive(Debug, Props, PartialEq)]
struct PropsC<'src> {
val: &'src str,
}
2021-03-13 15:02:57 +00:00
2021-03-14 00:11:06 +00:00
fn CustomC<'a>(ctx: Context<'a>, props: &'a PropsC<'a>) -> DomTree {
ctx.render(rsx! {
div {
class: "m-8"
"CustomC {props.val}"
}
})
}
2021-03-13 15:02:57 +00:00
}