Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeRef on_load panics when ref is remounted #2526

Open
DanielleHuisman opened this issue Apr 14, 2024 · 2 comments
Open

NodeRef on_load panics when ref is remounted #2526

DanielleHuisman opened this issue Apr 14, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@DanielleHuisman
Copy link

DanielleHuisman commented Apr 14, 2024

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.

@gbj gbj added the documentation Improvements or additions to documentation label Apr 15, 2024
@gbj
Copy link
Collaborator

gbj commented Apr 15, 2024

  1. You should use .get() (see example)
  2. .on_load()'s behavior of only working once should be documented properly.
  3. The warning is safe to ignore in this case, of course, but if you think it would be useful to have a way to opt out of it feel free to make a PR.

@DanielleHuisman
Copy link
Author

  1. Thanks for the advice! I managed to fix the code that my example was modeled after.
  2. Changing the unwrap to an expect with message in NodeRef.on_load might also help in documenting this behaviour.
  3. The warnings are mildly annoying during development, so I will probably create a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants