sketch.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use bumpalo::Bump;
  2. use dioxus_core::prelude::{Context, VNode};
  3. use std::{any::Any, cell::RefCell, rc::Rc};
  4. use std::{borrow::Borrow, sync::atomic::AtomicUsize};
  5. use typed_arena::Arena;
  6. fn main() {
  7. let ar = Arena::new();
  8. (0..5).for_each(|f| {
  9. // Create the temporary context obect
  10. let c = Context {
  11. _p: std::marker::PhantomData {},
  12. props: (),
  13. idx: 0.into(),
  14. arena: &ar,
  15. hooks: RefCell::new(Vec::new()),
  16. };
  17. component(c);
  18. });
  19. }
  20. // we need to do something about props and context being borrowed from different sources....
  21. // kinda anooying
  22. /// use_ref creates a new value when the component is created and then borrows that value on every render
  23. fn component(ctx: Context<()>) {
  24. (0..10).for_each(|f| {
  25. let r = use_ref(&ctx, move || f);
  26. assert_eq!(*r, f);
  27. });
  28. }
  29. pub fn use_ref<'a, P, T: 'static>(
  30. ctx: &'a Context<'a, P>,
  31. initial_state_fn: impl FnOnce() -> T + 'static,
  32. ) -> &'a T {
  33. ctx.use_hook(
  34. || initial_state_fn(), // initializer
  35. |state| state, // runner, borrows the internal value
  36. |b| {}, // tear down
  37. )
  38. }