docs: move around examples
This commit is contained in:
1 parent
b9ff95fa12
commit
70cd46dbb2
45 files changed
+3742
-2291
No files matched your search
@@ -4,14 +4,12 @@
|
||||
//! 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 std::{borrow::Borrow, ops::Deref, rc::Rc};
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
use std::rc::Rc;
|
||||
|
||||
struct Props {
|
||||
struct AppProps {
|
||||
items: Vec<Rc<ListItem>>,
|
||||
}
|
||||
|
||||
@@ -21,67 +19,54 @@ struct ListItem {
|
||||
age: u32,
|
||||
}
|
||||
|
||||
fn app(ctx: Context<Props>) -> VNode {
|
||||
fn app(ctx: Context<AppProps>) -> VNode {
|
||||
let (val, set_val) = use_state(&ctx, || 0);
|
||||
|
||||
ctx.render(dioxus::prelude::LazyNodes::new(move |c| {
|
||||
let mut root = builder::ElementBuilder::new(c, "div");
|
||||
for child in &ctx.items {
|
||||
// notice that the child directly borrows from our vec
|
||||
// this makes lists very fast (simply views reusing lifetimes)
|
||||
// <ChildItem item=child hanldler=setter />
|
||||
root = root.child(builder::virtual_child(
|
||||
c,
|
||||
ChildItem,
|
||||
// create the props with nothing but the fc<T>
|
||||
fc_to_builder(ChildItem)
|
||||
.item(child.clone())
|
||||
.item_handler(Callback(set_val.clone()))
|
||||
.build(),
|
||||
None,
|
||||
&[],
|
||||
));
|
||||
}
|
||||
root.finish()
|
||||
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
|
||||
#[derive(Props, PartialEq)]
|
||||
struct ChildProps {
|
||||
// Pass down complex structs
|
||||
item: Rc<ListItem>,
|
||||
|
||||
// Even pass down handlers!
|
||||
item_handler: Callback<i32>,
|
||||
item_handler: Rc<dyn Fn(i32)>,
|
||||
}
|
||||
|
||||
fn ChildItem<'a>(ctx: Context<ChildProps>) -> VNode {
|
||||
ctx.render(rsx! {
|
||||
div {
|
||||
// onclick: move |evt| (ctx.item_handler)(10)
|
||||
h1 { "abcd123 {ctx.item.name}" }
|
||||
h2 { "abcd123" }
|
||||
div {
|
||||
"abcd123"
|
||||
h2 { }
|
||||
p { }
|
||||
}
|
||||
}
|
||||
})
|
||||
fn ChildItem<'a>(cx: Context<'a, ChildProps>) -> VNode {
|
||||
cx.render(LazyNodes::new(move |__cx| todo!()))
|
||||
}
|
||||
|
||||
#[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> {
|
||||
impl PartialEq for ChildProps {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Rc::ptr_eq(&self.0, &other.0)
|
||||
false
|
||||
}
|
||||
}
|
||||
unsafe impl Properties for ChildProps {
|
||||
type Builder = ();
|
||||
const CAN_BE_MEMOIZED: bool = false;
|
||||
fn builder() -> Self::Builder {
|
||||
()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user