leptos icon indicating copy to clipboard operation
leptos copied to clipboard

NodeRef on_load panics when ref is remounted

Open DanielleHuisman opened this issue 4 months ago • 2 comments

Describe the bug When a ref is remounted, for example inside a <Show>, the on_load handler panics. The issue appears to be that on_load uses Cell.take() in the render effect, so the closure is no longer available when the ref is remounted.

 panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/leptos_dom-0.6.11/src/node_ref.rs:172:26:
called `Option::unwrap()` on a `None` value

The NodeRef also logs a warning when remounting refs:

 You are setting a NodeRef that has already been filled. It’s possible this is intentional, but it’s also possible that you’re accidentally using the same NodeRef for multiple _ref attributes.

I think having a ref to a toggable element is a valid use case, but maybe I'm wrong there.

Leptos Dependencies I first spotted the bug when using 0.6.9, but the CodeSandbox uses this:

leptos = { version = "0.6.11", features = ["csr", "nightly"] }

To Reproduce I made a small example in CodeSandbox to reproduce it: https://codesandbox.io/p/devbox/upbeat-johnson-h4mcsc.

In the CodeSandbox:

  1. Open the console.
  2. Click on "Hide" and again on "Show".
  3. See error in console.

Expected behavior I would expect on_load to be called again when the ref is remounted. It would also be nice if the NodeRef warnings could be disabled. For example, by having some way to indicate that a ref is allowed to be remounted.

DanielleHuisman avatar Apr 14 '24 10:04 DanielleHuisman