Feat: found a fast solution to hook state
This commit is contained in:
1 parent
62d4ad5878
commit
4d01436c3f
9 files changed
+759
-85
No files matched your search
@@ -0,0 +1,43 @@
|
||||
use bumpalo::Bump;
|
||||
use dioxus_core::prelude::{Context, VNode};
|
||||
use std::{any::Any, cell::RefCell, rc::Rc};
|
||||
use std::{borrow::Borrow, sync::atomic::AtomicUsize};
|
||||
use typed_arena::Arena;
|
||||
|
||||
fn main() {
|
||||
let ar = Arena::new();
|
||||
|
||||
(0..5).for_each(|f| {
|
||||
// Create the temporary context obect
|
||||
let c = Context {
|
||||
_p: std::marker::PhantomData {},
|
||||
props: (),
|
||||
idx: 0.into(),
|
||||
arena: &ar,
|
||||
hooks: RefCell::new(Vec::new()),
|
||||
};
|
||||
|
||||
component(c);
|
||||
});
|
||||
}
|
||||
|
||||
// we need to do something about props and context being borrowed from different sources....
|
||||
// kinda anooying
|
||||
/// use_ref creates a new value when the component is created and then borrows that value on every render
|
||||
fn component(ctx: Context<()>) {
|
||||
(0..10).for_each(|f| {
|
||||
let r = use_ref(&ctx, move || f);
|
||||
assert_eq!(*r, f);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn use_ref<'a, P, T: 'static>(
|
||||
ctx: &'a Context<'a, P>,
|
||||
initial_state_fn: impl FnOnce() -> T + 'static,
|
||||
) -> &'a T {
|
||||
ctx.use_hook(
|
||||
|| initial_state_fn(), // initializer
|
||||
|state| state, // runner, borrows the internal value
|
||||
|b| {}, // tear down
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user