2021-02-21 03:57:13 +00:00
|
|
|
//! Demonstrate that borrowed data is possible as a property type
|
|
|
|
//! Borrowing (rather than cloning) is very important for speed and ergonomics.
|
2021-02-22 18:10:36 +00:00
|
|
|
//!
|
|
|
|
//! It's slightly more advanced than just cloning, but well worth the investment.
|
|
|
|
//!
|
|
|
|
//! If you use the FC macro, we handle the lifetimes automatically, making it easy to write efficient & performant components.
|
2021-02-21 03:57:13 +00:00
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
|
2021-05-31 22:55:56 +00:00
|
|
|
use std::{borrow::Borrow, ops::Deref, rc::Rc};
|
2021-03-26 19:50:28 +00:00
|
|
|
|
2021-03-09 19:45:52 +00:00
|
|
|
use dioxus_core::prelude::*;
|
2021-02-21 03:57:13 +00:00
|
|
|
|
|
|
|
struct Props {
|
2021-05-31 22:55:56 +00:00
|
|
|
items: Vec<Rc<ListItem>>,
|
2021-02-21 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 05:58:20 +00:00
|
|
|
#[derive(PartialEq)]
|
2021-02-21 03:57:13 +00:00
|
|
|
struct ListItem {
|
|
|
|
name: String,
|
|
|
|
age: u32,
|
|
|
|
}
|
|
|
|
|
2021-06-01 22:33:15 +00:00
|
|
|
fn app(ctx: Context<Props>) -> VNode {
|
2021-03-29 16:31:47 +00:00
|
|
|
let (val, set_val) = use_state(&ctx, || 0);
|
2021-03-01 02:21:17 +00:00
|
|
|
|
2021-03-23 03:52:54 +00:00
|
|
|
ctx.render(dioxus::prelude::LazyNodes::new(move |c| {
|
2021-03-09 05:58:20 +00:00
|
|
|
let mut root = builder::ElementBuilder::new(c, "div");
|
2021-06-01 22:33:15 +00:00
|
|
|
for child in &ctx.items {
|
2021-02-21 03:57:13 +00:00
|
|
|
// notice that the child directly borrows from our vec
|
|
|
|
// this makes lists very fast (simply views reusing lifetimes)
|
2021-03-09 05:58:20 +00:00
|
|
|
// <ChildItem item=child hanldler=setter />
|
2021-02-21 03:57:13 +00:00
|
|
|
root = root.child(builder::virtual_child(
|
2021-03-09 05:58:20 +00:00
|
|
|
c,
|
2021-03-09 19:45:52 +00:00
|
|
|
ChildItem,
|
|
|
|
// create the props with nothing but the fc<T>
|
|
|
|
fc_to_builder(ChildItem)
|
2021-05-31 22:55:56 +00:00
|
|
|
.item(child.clone())
|
|
|
|
.item_handler(Callback(set_val.clone()))
|
2021-03-09 19:45:52 +00:00
|
|
|
.build(),
|
2021-04-01 04:01:42 +00:00
|
|
|
None,
|
2021-06-10 05:01:53 +00:00
|
|
|
&[],
|
2021-02-21 03:57:13 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
root.finish()
|
2021-03-23 03:52:54 +00:00
|
|
|
}))
|
2021-02-21 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 05:58:20 +00:00
|
|
|
// props should derive a partialeq implementation automatically, but implement ptr compare for & fields
|
2021-05-31 22:55:56 +00:00
|
|
|
#[derive(Props, PartialEq)]
|
|
|
|
struct ChildProps {
|
2021-03-01 02:21:17 +00:00
|
|
|
// Pass down complex structs
|
2021-05-31 22:55:56 +00:00
|
|
|
item: Rc<ListItem>,
|
2021-03-01 02:21:17 +00:00
|
|
|
|
|
|
|
// Even pass down handlers!
|
2021-05-31 22:55:56 +00:00
|
|
|
item_handler: Callback<i32>,
|
2021-03-09 05:58:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 22:33:15 +00:00
|
|
|
fn ChildItem<'a>(ctx: Context<ChildProps>) -> VNode {
|
2021-03-01 02:21:17 +00:00
|
|
|
ctx.render(rsx! {
|
|
|
|
div {
|
2021-06-01 22:33:15 +00:00
|
|
|
// onclick: move |evt| (ctx.item_handler)(10)
|
|
|
|
h1 { "abcd123 {ctx.item.name}" }
|
2021-03-26 19:50:28 +00:00
|
|
|
h2 { "abcd123" }
|
2021-03-01 02:21:17 +00:00
|
|
|
div {
|
2021-03-26 19:50:28 +00:00
|
|
|
"abcd123"
|
|
|
|
h2 { }
|
|
|
|
p { }
|
|
|
|
}
|
2021-03-01 02:21:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-05-31 22:55:56 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Callback<I, O = ()>(Rc<dyn Fn(I) -> O>);
|
|
|
|
impl<I, O> Deref for Callback<I, O> {
|
|
|
|
type Target = Rc<dyn Fn(I) -> O>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<I, O> PartialEq for Callback<I, O> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
Rc::ptr_eq(&self.0, &other.0)
|
|
|
|
}
|
|
|
|
}
|