2022-01-25 00:52:12 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2021-09-24 04:05:56 +00:00
|
|
|
/*
|
|
|
|
Dioxus manages borrow lifetimes for you. This means any child may borrow from its parent. However, it is not possible
|
|
|
|
to hand out an &mut T to children - all props are consumed by &P, so you'd only get an &&mut T.
|
|
|
|
|
|
|
|
How does it work?
|
|
|
|
|
|
|
|
Dioxus will manually drop closures and props - things that borrow data before the component is ran again. This is done
|
|
|
|
"bottom up" from the lowest child all the way to the initiating parent. As it traverses each listener and prop, the
|
|
|
|
drop implementation is manually called, freeing any memory and ensuring that memory is not leaked.
|
|
|
|
|
|
|
|
We cannot drop from the parent to the children - if the drop implementation modifies the data, downstream references
|
|
|
|
might be broken since we take an &mut T and and &T to the data. Instead, we work bottom up, making sure to remove any
|
|
|
|
potential references to the data before finally giving out an &mut T. This prevents us from mutably aliasing the data,
|
|
|
|
and is proven to be safe with MIRI.
|
|
|
|
*/
|
2021-08-06 02:23:41 +00:00
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(app);
|
2021-08-06 02:23:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 23:35:38 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-07-11 19:50:56 +00:00
|
|
|
let text = cx.use_hook(|| vec![String::from("abc=def")]);
|
2021-08-06 02:23:41 +00:00
|
|
|
|
|
|
|
let first = text.get_mut(0).unwrap();
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
Child1 {
|
|
|
|
text: first
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
struct C1Props<'a> {
|
|
|
|
text: &'a mut String,
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:56:53 +00:00
|
|
|
fn Child1<'a>(cx: Scope<'a, C1Props<'a>>) -> Element {
|
2022-02-28 07:38:17 +00:00
|
|
|
let (left, right) = cx.props.text.split_once('=').unwrap();
|
2021-08-06 02:23:41 +00:00
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
Child2 { text: left }
|
|
|
|
Child2 { text: right }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
struct C2Props<'a> {
|
|
|
|
text: &'a str,
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:56:53 +00:00
|
|
|
fn Child2<'a>(cx: Scope<'a, C2Props<'a>>) -> Element {
|
2021-08-06 02:23:41 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
Child3 {
|
2021-12-25 22:18:05 +00:00
|
|
|
text: cx.props.text
|
2021-08-06 02:23:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
struct C3Props<'a> {
|
|
|
|
text: &'a str,
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:56:53 +00:00
|
|
|
fn Child3<'a>(cx: Scope<'a, C3Props<'a>>) -> Element {
|
2021-08-06 02:23:41 +00:00
|
|
|
cx.render(rsx! {
|
2021-12-15 20:56:53 +00:00
|
|
|
div { "{cx.props.text}"}
|
2021-08-06 02:23:41 +00:00
|
|
|
})
|
|
|
|
}
|