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-03-09 05:58:20 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
use dioxus_core::{component::Properties, prelude::*};
|
2021-02-21 03:57:13 +00:00
|
|
|
|
|
|
|
struct Props {
|
|
|
|
items: Vec<ListItem>,
|
|
|
|
}
|
|
|
|
|
2021-03-09 05:58:20 +00:00
|
|
|
#[derive(PartialEq)]
|
2021-02-21 03:57:13 +00:00
|
|
|
struct ListItem {
|
|
|
|
name: String,
|
|
|
|
age: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn app(ctx: Context, props: &Props) -> DomTree {
|
2021-03-01 02:21:17 +00:00
|
|
|
let (f, setter) = use_state(&ctx, || 0);
|
|
|
|
|
2021-03-09 05:58:20 +00:00
|
|
|
ctx.render(move |c| {
|
|
|
|
let mut root = builder::ElementBuilder::new(c, "div");
|
2021-02-21 03:57:13 +00:00
|
|
|
for child in &props.items {
|
|
|
|
// 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-01 02:21:17 +00:00
|
|
|
ChildProps {
|
|
|
|
item: child,
|
|
|
|
item_handler: setter,
|
|
|
|
},
|
2021-02-21 03:57:13 +00:00
|
|
|
child_item,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
root.finish()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-01 02:21:17 +00:00
|
|
|
type StateSetter<T> = dyn Fn(T);
|
2021-03-09 05:58:20 +00:00
|
|
|
// struct StateSetter<T>(dyn Fn(T));
|
|
|
|
|
|
|
|
// impl<T> PartialEq for StateSetter<T> {
|
|
|
|
// fn eq(&self, other: &Self) -> bool {
|
|
|
|
// self as *const _ == other as *const _
|
|
|
|
// }
|
|
|
|
// }
|
2021-03-01 02:21:17 +00:00
|
|
|
|
2021-03-09 05:58:20 +00:00
|
|
|
// props should derive a partialeq implementation automatically, but implement ptr compare for & fields
|
|
|
|
#[derive(Props)]
|
2021-02-21 03:57:13 +00:00
|
|
|
struct ChildProps<'a> {
|
2021-03-01 02:21:17 +00:00
|
|
|
// Pass down complex structs
|
2021-02-21 03:57:13 +00:00
|
|
|
item: &'a ListItem,
|
2021-03-01 02:21:17 +00:00
|
|
|
|
|
|
|
// Even pass down handlers!
|
|
|
|
item_handler: &'a StateSetter<i32>,
|
2021-02-21 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 05:58:20 +00:00
|
|
|
impl PartialEq for ChildProps<'_> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Properties for ChildProps<'a> {
|
|
|
|
type Builder = ChildPropsBuilder<'a, ((), ())>;
|
|
|
|
fn builder() -> <Self as Properties>::Builder {
|
|
|
|
ChildProps::builder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 02:21:17 +00:00
|
|
|
fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
|
2021-02-21 03:57:13 +00:00
|
|
|
todo!()
|
2021-03-01 02:21:17 +00:00
|
|
|
// ctx.render(rsx! {
|
|
|
|
// div {
|
|
|
|
// item: child,
|
|
|
|
// handler: setter,
|
|
|
|
// abc: 123,
|
|
|
|
// onclick: props.item_handler,
|
|
|
|
|
|
|
|
// h1 { "abcd123" }
|
|
|
|
// h2 { "abcd123" }
|
|
|
|
// div {
|
|
|
|
// "abcd123"
|
|
|
|
// h2 { }
|
|
|
|
// p { }
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rsx! {
|
|
|
|
ChildItem {
|
|
|
|
// props
|
|
|
|
item: child, handler: setter,
|
|
|
|
|
|
|
|
// children
|
|
|
|
div { class:"abcd", abc: 123 },
|
|
|
|
div { class:"abcd", abc: 123 },
|
|
|
|
|
|
|
|
// Auto-text coercion
|
|
|
|
"eyo matie {abc}",
|
|
|
|
|
|
|
|
// Anything that accepts Into<VChild>
|
|
|
|
{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dreaming of this syntax
|
|
|
|
#[derive(Properties)]
|
|
|
|
struct ChildProps<'a> {
|
|
|
|
username: &'a str,
|
|
|
|
item_handler: &'a dyn Fn(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
|
|
|
|
ctx.render(rsx! {
|
|
|
|
div {
|
|
|
|
class: "abc123",
|
|
|
|
abc: 123,
|
|
|
|
onclick: props.item_handler,
|
|
|
|
|
|
|
|
h1 { "Hello, {props.username}!" },
|
|
|
|
h2 { "Welcome the RSX syntax" },
|
|
|
|
div {
|
|
|
|
h3 { "This is a subheader" }
|
|
|
|
button {
|
|
|
|
onclick: props.handler,
|
|
|
|
"This is a button"
|
|
|
|
}
|
|
|
|
"This is child text"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is also nice
|
|
|
|
|
|
|
|
#[dioxus::component]
|
|
|
|
static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
|
|
|
|
ctx.render(rsx! {
|
|
|
|
div {
|
|
|
|
class: "abc123",
|
|
|
|
abc: 123,
|
|
|
|
onclick: handler,
|
|
|
|
|
|
|
|
h1 { "Hello, {username}!" },
|
|
|
|
h2 { "Welcome the RSX syntax" },
|
|
|
|
div {
|
|
|
|
h3 { "This is a subheader" }
|
|
|
|
button {
|
|
|
|
onclick: props.handler,
|
|
|
|
"This is a button"
|
|
|
|
}
|
|
|
|
"This is child text"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2021-02-21 03:57:13 +00:00
|
|
|
}
|
2021-03-01 02:21:17 +00:00
|
|
|
Menlo, Monaco, 'Courier New', monospace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Item {
|
|
|
|
name: String,
|
|
|
|
content: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[dioxus::live_component]
|
|
|
|
static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
|
|
|
|
// return lazy nodes or
|
|
|
|
let ssr = ctx.suspend(async {
|
|
|
|
let data = fetch("https://google.com")
|
|
|
|
.await?
|
|
|
|
.json::<Item>()
|
|
|
|
.await?;
|
|
|
|
rsx! {
|
|
|
|
div {
|
|
|
|
h1 { "Welcome: {data.name}" }
|
|
|
|
p { "Content: \n {data.content}" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.render(rsx! {
|
|
|
|
div {
|
|
|
|
class: "abc123",
|
|
|
|
abc: 123,
|
|
|
|
onclick: handler,
|
|
|
|
|
|
|
|
h1 { "Hello, {username}!" },
|
|
|
|
h2 { "Welcome the RSX syntax" },
|
|
|
|
div {
|
|
|
|
h3 { "This is a subheader" }
|
|
|
|
button {
|
|
|
|
onclick: props.handler,
|
|
|
|
"This is a button"
|
|
|
|
}
|
|
|
|
{ssr}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|