WIP: move to static props
This commit is contained in:
1 parent
c5089ba3c5
commit
c1fd848f89
37 files changed
+1014
-339
No files matched your search
@@ -7,12 +7,12 @@
|
||||
|
||||
fn main() {}
|
||||
|
||||
use std::{borrow::Borrow, rc::Rc};
|
||||
use std::{borrow::Borrow, ops::Deref, rc::Rc};
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
|
||||
struct Props {
|
||||
items: Vec<ListItem>,
|
||||
items: Vec<Rc<ListItem>>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
@@ -35,8 +35,8 @@ fn app<'a>(ctx: Context<'a>, props: &Props) -> DomTree {
|
||||
ChildItem,
|
||||
// create the props with nothing but the fc<T>
|
||||
fc_to_builder(ChildItem)
|
||||
.item(child)
|
||||
.item_handler(set_val.clone())
|
||||
.item(child.clone())
|
||||
.item_handler(Callback(set_val.clone()))
|
||||
.build(),
|
||||
None,
|
||||
));
|
||||
@@ -46,19 +46,13 @@ fn app<'a>(ctx: Context<'a>, props: &Props) -> DomTree {
|
||||
}
|
||||
|
||||
// props should derive a partialeq implementation automatically, but implement ptr compare for & fields
|
||||
#[derive(Props)]
|
||||
struct ChildProps<'a> {
|
||||
#[derive(Props, PartialEq)]
|
||||
struct ChildProps {
|
||||
// Pass down complex structs
|
||||
item: &'a ListItem,
|
||||
item: Rc<ListItem>,
|
||||
|
||||
// Even pass down handlers!
|
||||
item_handler: Rc<dyn Fn(i32)>,
|
||||
}
|
||||
|
||||
impl PartialEq for ChildProps<'_> {
|
||||
fn eq(&self, _other: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
item_handler: Callback<i32>,
|
||||
}
|
||||
|
||||
fn ChildItem<'a>(ctx: Context<'a>, props: &ChildProps) -> DomTree {
|
||||
@@ -75,3 +69,18 @@ fn ChildItem<'a>(ctx: Context<'a>, props: &ChildProps) -> DomTree {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
use std::{ops::Deref, rc::Rc};
|
||||
|
||||
use dioxus::virtual_dom::Scope;
|
||||
use dioxus_core::prelude::*;
|
||||
|
||||
type RcStr = Rc<str>;
|
||||
|
||||
fn main() {
|
||||
let r: RcStr = "asdasd".into();
|
||||
let r: RcStr = String::from("asdasd").into();
|
||||
|
||||
let g = rsx! {
|
||||
div {
|
||||
Example {}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static Example: FC<()> = |ctx, props| {
|
||||
let nodes = ctx.children();
|
||||
|
||||
//
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
{nodes}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct MyContext<'a, T> {
|
||||
props: &'a T,
|
||||
inner: &'a Scope,
|
||||
}
|
||||
impl<'a, T> MyContext<'a, T> {
|
||||
fn children(&self) -> Vec<VNode<'a>> {
|
||||
todo!()
|
||||
}
|
||||
pub fn render2<F: for<'b> FnOnce(&'b NodeCtx<'a>) -> VNode<'a> + 'a>(
|
||||
&self,
|
||||
lazy_nodes: LazyNodes<'a, F>,
|
||||
) -> VNode<'a> {
|
||||
self.inner.render2(lazy_nodes)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Deref for MyContext<'a, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.props
|
||||
}
|
||||
}
|
||||
|
||||
struct MyProps {
|
||||
title: String,
|
||||
}
|
||||
|
||||
fn example(scope: MyContext<MyProps>) -> VNode {
|
||||
let childs = scope.children();
|
||||
|
||||
scope.inner.render2(rsx! {
|
||||
div {
|
||||
"{scope.title}"
|
||||
{childs}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core_macro::rsx_template;
|
||||
|
||||
fn main() {
|
||||
let g = html!("<div> </div>");
|
||||
let g = html!("<div> </div>");
|
||||
// let g = rsx!("div { div { } } ");
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core_macro::format_args_f;
|
||||
|
||||
fn main() {
|
||||
let num = 123;
|
||||
let b = Bump::new();
|
||||
|
||||
let g = rsx! {
|
||||
div {
|
||||
"abc {num}"
|
||||
div {
|
||||
"asd"
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user