Skip to content

Commit

Permalink
docs: add warning for nested Fn in attribute (see #2023) (#2045)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj authored Nov 22, 2023
1 parent 1cd6603 commit 0ce4ee8
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions leptos_dom/src/macro_helpers/into_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,24 +386,41 @@ attr_signal_type_optional!(MaybeProp<T>);
#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[doc(hidden)]
#[inline(never)]
#[track_caller]
pub fn attribute_helper(
el: &web_sys::Element,
name: Oco<'static, str>,
value: Attribute,
) {
#[cfg(debug_assertions)]
let called_at = std::panic::Location::caller();
use leptos_reactive::create_render_effect;
match value {
Attribute::Fn(f) => {
let el = el.clone();
create_render_effect(move |old| {
let new = f();
if old.as_ref() != Some(&new) {
attribute_expression(&el, &name, new.clone(), true);
attribute_expression(
&el,
&name,
new.clone(),
true,
#[cfg(debug_assertions)]
called_at,
);
}
new
});
}
_ => attribute_expression(el, &name, value, false),
_ => attribute_expression(
el,
&name,
value,
false,
#[cfg(debug_assertions)]
called_at,
),
};
}

Expand All @@ -414,6 +431,7 @@ pub(crate) fn attribute_expression(
attr_name: &str,
value: Attribute,
force: bool,
#[cfg(debug_assertions)] called_at: &'static std::panic::Location<'static>,
) {
use crate::HydrationCtx;

Expand Down Expand Up @@ -452,10 +470,25 @@ pub(crate) fn attribute_expression(
}
Attribute::Fn(f) => {
let mut v = f();
crate::debug_warn!(
"At {called_at}, you are providing a dynamic attribute \
with a nested function. For example, you might have a \
closure that returns another function instead of a \
value. This creates some added overhead. If possible, \
you should instead provide a function that returns a \
value instead.",
);
while let Attribute::Fn(f) = v {
v = f();
}
attribute_expression(el, attr_name, v, force);
attribute_expression(
el,
attr_name,
v,
force,
#[cfg(debug_assertions)]
called_at,
);
}
}
}
Expand Down

0 comments on commit 0ce4ee8

Please sign in to comment.