fixed .into_view(cx) calling order to match between SSR and CSR

This commit is contained in:
Jose Quesada 2022-12-08 10:41:28 -06:00
parent 61e206c227
commit d29d29c1d4
3 changed files with 25 additions and 44 deletions

View file

@ -162,13 +162,7 @@ where
}
#[cfg(all(target_arch = "wasm32", feature = "web"))]
{
if cfg!(feature = "hydrate") && !HydrationCtx::is_hydrating() {
mount_child(MountKind::Before(&closing), &new_child);
} else {
mount_child(MountKind::Before(&closing), &new_child);
}
}
mount_child(MountKind::Before(&closing), &new_child);
**child.borrow_mut() = Some(new_child);
}

View file

@ -158,7 +158,7 @@ cfg_if! {
pub(crate) attrs: SmallVec<[(Cow<'static, str>, Cow<'static, str>); 4]>,
#[educe(Debug(ignore))]
#[allow(clippy::type_complexity)]
pub(crate) children: SmallVec<[Box<dyn FnOnce(Scope) -> View>; 4]>,
pub(crate) children: SmallVec<[View; 4]>,
}
}
}
@ -434,24 +434,13 @@ impl<El: IntoElement> HtmlElement<El> {
#[doc(hidden)]
#[track_caller]
pub fn child(mut self, child: impl IntoChild) -> Self {
let child = child.into_child(self.cx);
let child = child.into_child(self.cx).into_view(self.cx);
cfg_if! {
if #[cfg(all(target_arch = "wasm32", feature = "web"))] {
#[cfg(feature = "hydrate")]
{
// if we're currently hydrating, we don't need to mount the view,
// but we still need to run into_view(), or it may not generate an ID
let view = child.into_view(self.cx);
if !HydrationCtx::is_hydrating() {
mount_child(MountKind::Append(self.element.get_element()), &view)
}
}
#[cfg(not(feature = "hydrate"))]
mount_child(MountKind::Append(self.element.get_element()), &child.into_view(self.cx))
mount_child(MountKind::Append(self.element.get_element()), &child);
}
else {
self.children.push(Box::new(move |cx| child.into_view(cx)));
self.children.push(child);
}
}
@ -471,10 +460,7 @@ impl<El: IntoElement> IntoView for HtmlElement<El> {
let id = element.hydration_id();
let mut element = Element::new(element);
let children = children
.into_iter()
.map(|c| c(cx))
.collect::<SmallVec<[_; 4]>>();
let children = children;
if !attrs.iter_mut().any(|(name, _)| name == "id") {
attrs.push(("id".into(), format!("_{}", id).into()));

View file

@ -345,24 +345,26 @@ impl View {
#[track_caller]
#[cfg(all(target_arch = "wasm32", feature = "web"))]
fn mount_child<GWSN: Mountable + fmt::Debug>(kind: MountKind, child: &GWSN) {
let child = child.get_mountable_node();
if !HydrationCtx::is_hydrating() {
let child = child.get_mountable_node();
match kind {
MountKind::Append(el) => {
el.append_child(&child)
.expect("append operation to not err");
}
MountKind::Before(closing) => {
closing
.unchecked_ref::<web_sys::Element>()
.before_with_node_1(&child)
.expect("before to not err");
}
MountKind::After(closing) => {
closing
.unchecked_ref::<web_sys::Element>()
.after_with_node_1(&child)
.expect("before to not err");
match kind {
MountKind::Append(el) => {
el.append_child(&child)
.expect("append operation to not err");
}
MountKind::Before(closing) => {
closing
.unchecked_ref::<web_sys::Element>()
.before_with_node_1(&child)
.expect("before to not err");
}
MountKind::After(closing) => {
closing
.unchecked_ref::<web_sys::Element>()
.after_with_node_1(&child)
.expect("before to not err");
}
}
}
}
@ -403,7 +405,6 @@ where
move |cx| {
let node = f(cx).into_view(cx);
#[cfg(all(feature = "web", feature = "hydrate"))]
HydrationCtx::stop_hydrating();
parent.append_child(&node.get_mountable_node()).unwrap();