diff --git a/packages/yew/src/html/component/scope.rs b/packages/yew/src/html/component/scope.rs index c03752ae0e7..4f4e6eab1a1 100644 --- a/packages/yew/src/html/component/scope.rs +++ b/packages/yew/src/html/component/scope.rs @@ -392,12 +392,12 @@ mod feat_csr { ComponentRenderState, CreateRunner, DestroyRunner, RenderRunner, }; use crate::html::{CompAnyRef, NodeRef}; - use crate::{scheduler, ComponentRef}; + use crate::scheduler; use std::cell::Ref; use web_sys::Element; pub(crate) struct ScopeHolder { - pub scope_ref: ComponentRef, + pub scope_ref: CompAnyRef, pub scope: Scope, } @@ -484,7 +484,7 @@ mod feat_csr { self.scope.clone().into() } fn scope_ref(&self) -> CompAnyRef { - self.scope_ref.to_any() + self.scope_ref.clone() } fn render_state(&self) -> Option> { let state_ref = self.scope.state.borrow(); diff --git a/packages/yew/src/html/mod.rs b/packages/yew/src/html/mod.rs index 4a47c0538d3..fae98994e7f 100644 --- a/packages/yew/src/html/mod.rs +++ b/packages/yew/src/html/mod.rs @@ -172,6 +172,7 @@ mod feat_csr { } } + #[derive(Clone)] pub(crate) struct CompAnyRef(Rc>); impl PartialEq> for CompAnyRef { @@ -189,15 +190,18 @@ mod feat_csr { } impl ComponentRef { - /// Place a scope in a reference for later use - pub(crate) fn set(&self, scope: Option>) { - let mut this = self.0.borrow_mut(); - this.scope = scope.map(|s| s.into()); - } /// Convert to type erased, inner form pub(crate) fn to_any(&self) -> CompAnyRef { CompAnyRef(self.0.clone()) } + } + + impl CompAnyRef { + /// Place a scope in a reference for later use + pub(crate) fn set(&self, scope: Option) { + let mut this = self.0.borrow_mut(); + this.scope = scope; + } /// Re-use the old inner reference. /// MUST BE SURE THAT THE COMPONENT TYPES MATCH /// for example, by downcasting a Scope of the same type first diff --git a/packages/yew/src/virtual_dom/vcomp.rs b/packages/yew/src/virtual_dom/vcomp.rs index ad39f7990be..f06dcb91191 100644 --- a/packages/yew/src/virtual_dom/vcomp.rs +++ b/packages/yew/src/virtual_dom/vcomp.rs @@ -12,7 +12,7 @@ use crate::html::{AnyScope, Scope}; #[cfg(feature = "csr")] use crate::dom_bundle::BSubtree; #[cfg(feature = "csr")] -use crate::html::{NodeRef, Scoped}; +use crate::html::{CompAnyRef, NodeRef, Scoped}; #[cfg(feature = "csr")] use web_sys::Element; @@ -75,12 +75,15 @@ pub(crate) trait Mountable { pub(crate) struct PropsWrapper { props: Rc, - comp_ref: ComponentRef, + external_ref: CompAnyRef, } impl PropsWrapper { - pub fn new(props: Rc, comp_ref: ComponentRef) -> Self { - Self { props, comp_ref } + pub fn new(props: Rc, external_ref: ComponentRef) -> Self { + Self { + props, + external_ref: external_ref.to_any(), + } } } @@ -88,7 +91,7 @@ impl Mountable for PropsWrapper { fn copy(&self) -> Box { let wrapper: PropsWrapper = PropsWrapper { props: Rc::clone(&self.props), - comp_ref: self.comp_ref.clone(), + external_ref: self.external_ref.clone(), }; Box::new(wrapper) } @@ -105,20 +108,20 @@ impl Mountable for PropsWrapper { let scope: Scope = Scope::new(Some(parent_scope.clone())); scope.mount_in_place(root.clone(), parent, next_sibling, internal_ref, self.props); - self.comp_ref.set(Some(scope.clone())); + self.external_ref.set(Some(scope.clone().into())); Box::new(crate::html::ScopeHolder { scope, - scope_ref: self.comp_ref, + scope_ref: self.external_ref, }) } #[cfg(feature = "csr")] fn reuse(self: Box, scoped: &dyn Scoped, next_sibling: NodeRef) { let scope: Scope = scoped.to_any().downcast::(); - self.comp_ref.reuse_any(&scoped.scope_ref()); + self.external_ref.reuse_any(&scoped.scope_ref()); scope.reuse(self.props, next_sibling); - self.comp_ref.set(Some(scope)); + self.external_ref.set(Some(scope.into())); } #[cfg(feature = "ssr")] @@ -136,7 +139,7 @@ impl Mountable for PropsWrapper { #[cfg(all(test, feature = "csr"))] fn component_ref(&self) -> crate::html::CompAnyRef { - self.comp_ref.to_any() + self.external_ref.clone() } }