dioxus/packages/web/examples/deep.rs

75 lines
1.7 KiB
Rust
Raw Normal View History

use std::rc::Rc;
use dioxus_core as dioxus;
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
}
fn CustomA(ctx: Context<()>) -> VNode {
let (val, set_val) = use_state(&ctx, || "abcdef".to_string() as 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
}
2021-05-15 16:03:08 +00:00
components::CustomB {
val: val.clone()
2021-03-13 15:02:57 +00:00
}
}
})
}
2021-03-14 00:11:06 +00:00
mod components {
use std::rc::Rc;
2021-03-14 00:11:06 +00:00
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 {
val: String,
2021-03-14 00:11:06 +00:00
}
pub fn CustomB(ctx: Context<PropsB>) -> VNode {
let (first, last) = ctx.val.split_at(3);
2021-03-14 00:11:06 +00:00
ctx.render(rsx! {
div {
class: "m-8"
"CustomB {ctx.val}"
2021-03-14 00:11:06 +00:00
CustomC {
val: first.to_string()
2021-03-14 00:11:06 +00:00
}
CustomC {
val: last.to_string()
2021-03-14 00:11:06 +00:00
}
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 {
val: String,
2021-03-14 00:11:06 +00:00
}
2021-03-13 15:02:57 +00:00
fn CustomC(ctx: Context<PropsC>) -> VNode {
2021-03-14 00:11:06 +00:00
ctx.render(rsx! {
div {
class: "m-8"
"CustomC {ctx.val}"
2021-03-14 00:11:06 +00:00
}
})
}
2021-03-13 15:02:57 +00:00
}