diff --git a/packages/core/.vscode/settings.json b/packages/core/.vscode/settings.json index 1c68f97cc..79505b018 100644 --- a/packages/core/.vscode/settings.json +++ b/packages/core/.vscode/settings.json @@ -1,3 +1,3 @@ { - "rust-analyzer.inlayHints.enable": true + "rust-analyzer.inlayHints.enable": false } diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 7b50f2fe5..6ce6d5d05 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -42,8 +42,6 @@ pub struct VirtualDom { root_fc: Box, - root_prop_type: std::any::TypeId, - root_props: Pin>, } @@ -105,10 +103,8 @@ impl VirtualDom { pub fn new_with_props(root: FC

, root_props: P) -> Self { let scheduler = Scheduler::new(); - let _root_props: Pin> = Box::pin(root_props); - let _root_prop_type = TypeId::of::

(); - - let props_ptr = _root_props.downcast_ref::

().unwrap() as *const P; + let root_props: Pin> = Box::pin(root_props); + let props_ptr = root_props.downcast_ref::

().unwrap() as *const P; let base_scope = scheduler.pool.insert_scope_with_key(|myidx| { let caller = NodeFactory::create_component_caller(root, props_ptr as *const _); @@ -126,15 +122,19 @@ impl VirtualDom { root_fc: Box::new(root), base_scope, scheduler, - root_props: _root_props, - root_prop_type: _root_prop_type, + root_props, } } + /// Get the [`Scope`] for the root component. + /// + /// This is useful for traversing the tree from the root for heuristics or altnerative renderers that use Dioxus + /// directly. pub fn base_scope(&self) -> &Scope { self.scheduler.pool.get_scope(self.base_scope).unwrap() } + /// Get the [`Scope`] for a component given its [`ScopeId`] pub fn get_scope(&self, id: ScopeId) -> Option<&Scope> { self.scheduler.pool.get_scope(id) } @@ -143,15 +143,28 @@ impl VirtualDom { /// /// This method retuns None if the old props could not be removed. The entire VirtualDOM will be rebuilt immediately, /// so calling this method will block the main thread until computation is done. + /// + /// ## Example + /// + /// ```rust + /// #[derive(Props, PartialEq)] + /// struct AppProps { + /// route: &'static str + /// } + /// static App: FC = |cx| cx.render(rsx!{ "route is {cx.route}" }); + /// + /// let mut dom = VirtualDom::new_with_props(App, AppProps { route: "start" }); + /// + /// let mutations = dom.update_root_props(AppProps { route: "end" }).unwrap(); + /// ``` pub fn update_root_props<'s, P: 'static>(&'s mut self, root_props: P) -> Option> { let root_scope = self.scheduler.pool.get_scope_mut(self.base_scope).unwrap(); root_scope.ensure_drop_safety(&self.scheduler.pool); - let mut _root_props: Pin> = Box::pin(root_props); - let _root_prop_type = TypeId::of::

(); + let mut root_props: Pin> = Box::pin(root_props); - if let Some(props_ptr) = _root_props.downcast_ref::

().map(|p| p as *const P) { - std::mem::swap(&mut self.root_props, &mut _root_props); + if let Some(props_ptr) = root_props.downcast_ref::

().map(|p| p as *const P) { + std::mem::swap(&mut self.root_props, &mut root_props); let root = *self.root_fc.downcast_ref::>().unwrap();