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 {
let (val, set_val) = use_state(&ctx, || "abcdef".to_string());
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"
onclick: move |_| set_val(val.to_ascii_uppercase())
}
button {
"Lower"
onclick: move |_| set_val(val.to_ascii_lowercase())
}
CustomB {
val: val
}
}
})
}
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
}