mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-12-23 02:53:12 +00:00
73047fe956
This commit solves the memoization , properly memoizing properties that don't have any generic parameters. This is a rough heuristic to prevent non-static lifetimes from creeping into props and breaking our minual lifetime management. Props that have a generic parameter are opted-out of the `partialeq` requirement and props *without* lifetimes must implement partialeq. We're going to leave manual disabling of memoization for future work.
74 lines
2 KiB
Rust
74 lines
2 KiB
Rust
//! Demonstrate that borrowed data is possible as a property type
|
|
//! Borrowing (rather than cloning) is very important for speed and ergonomics.
|
|
//!
|
|
//! 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.
|
|
fn main() {}
|
|
|
|
use dioxus_core::prelude::*;
|
|
use std::rc::Rc;
|
|
|
|
struct AppProps {
|
|
items: Vec<Rc<ListItem>>,
|
|
}
|
|
|
|
#[derive(PartialEq)]
|
|
struct ListItem {
|
|
name: String,
|
|
age: u32,
|
|
}
|
|
|
|
fn app(ctx: Context<AppProps>) -> VNode {
|
|
let (val, set_val) = use_state(&ctx, || 0);
|
|
|
|
ctx.render(LazyNodes::new(move |_nodectx| {
|
|
builder::ElementBuilder::new(_nodectx, "div")
|
|
.iter_child({
|
|
ctx.items.iter().map(|child| {
|
|
builder::virtual_child(
|
|
_nodectx,
|
|
ChildItem,
|
|
ChildProps {
|
|
item: child.clone(),
|
|
item_handler: set_val.clone(),
|
|
},
|
|
None,
|
|
&[],
|
|
)
|
|
})
|
|
})
|
|
.iter_child([builder::ElementBuilder::new(_nodectx, "div")
|
|
.iter_child([builder::text3(_nodectx.bump(), format_args!("{}", val))])
|
|
.finish()])
|
|
.finish()
|
|
}))
|
|
}
|
|
|
|
// props should derive a partialeq implementation automatically, but implement ptr compare for & fields
|
|
struct ChildProps {
|
|
// Pass down complex structs
|
|
item: Rc<ListItem>,
|
|
|
|
// Even pass down handlers!
|
|
item_handler: Rc<dyn Fn(i32)>,
|
|
}
|
|
|
|
fn ChildItem<'a>(cx: Context<'a, ChildProps>) -> VNode {
|
|
cx.render(LazyNodes::new(move |__cx| todo!()))
|
|
}
|
|
|
|
impl PartialEq for ChildProps {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
false
|
|
}
|
|
}
|
|
impl Properties for ChildProps {
|
|
type Builder = ();
|
|
fn builder() -> Self::Builder {
|
|
()
|
|
}
|
|
unsafe fn memoize(&self, other: &Self) -> bool {
|
|
self == other
|
|
}
|
|
}
|