props_safety.rs 731 B

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