component_child.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use std::{ops::Deref, rc::Rc};
  2. use dioxus::virtual_dom::Scope;
  3. use dioxus_core::prelude::*;
  4. type RcStr = Rc<str>;
  5. fn main() {
  6. let r: RcStr = "asdasd".into();
  7. let r: RcStr = String::from("asdasd").into();
  8. let g = rsx! {
  9. div {
  10. Example {}
  11. }
  12. };
  13. }
  14. static Example: FC<()> = |ctx, props| {
  15. let nodes = ctx.children();
  16. //
  17. rsx! { in ctx,
  18. div {
  19. {nodes}
  20. }
  21. }
  22. };
  23. #[derive(Clone, Copy)]
  24. struct MyContext<'a, T> {
  25. props: &'a T,
  26. inner: &'a Scope,
  27. }
  28. impl<'a, T> MyContext<'a, T> {
  29. fn children(&self) -> Vec<VNode<'a>> {
  30. todo!()
  31. }
  32. pub fn render2<F: for<'b> FnOnce(&'b NodeCtx<'a>) -> VNode<'a> + 'a>(
  33. &self,
  34. lazy_nodes: LazyNodes<'a, F>,
  35. ) -> VNode<'a> {
  36. self.inner.render2(lazy_nodes)
  37. }
  38. }
  39. impl<'a, T> Deref for MyContext<'a, T> {
  40. type Target = T;
  41. fn deref(&self) -> &Self::Target {
  42. self.props
  43. }
  44. }
  45. struct MyProps {
  46. title: String,
  47. }
  48. fn example(scope: MyContext<MyProps>) -> VNode {
  49. let childs = scope.children();
  50. scope.inner.render2(rsx! {
  51. div {
  52. "{scope.title}"
  53. {childs}
  54. }
  55. })
  56. }