props_safety.rs 641 B

1234567891011121314151617181920212223242526
  1. use dioxus::prelude::*;
  2. fn main() {}
  3. fn app() -> Element {
  4. let count: &RefCell<Vec<Element>> = cx.use_hook(|| RefCell::new(Vec::new()));
  5. render! { unsafe_child_component { borrowed: count } }
  6. }
  7. #[derive(Props)]
  8. struct Testing<'a> {
  9. borrowed: &'a RefCell<Vec<Element>>,
  10. }
  11. fn unsafe_child_component<'a>(cx: Testing) -> Element {
  12. let Testing { borrowed } = cx;
  13. let borrowed_temporary_data =
  14. cx.use_hook(|| String::from("This data is only valid for the lifetime of the child"));
  15. borrowed
  16. .borrow_mut()
  17. .push(render! {"{borrowed_temporary_data}"});
  18. render! { div { "Hello, world!" } }
  19. }