Repository navigation
Conversation
`NodeRef` compares by identity, not by the node it holds (`impl PartialEq for NodeRef` uses `std::ptr::eq` on the shared `Rc`), and `use_effect_with` re-runs only when the dependency compares unequal. A ref that was still empty when the effect first ran therefore never triggers it again, even after the element mounts, so listeners and other setup work added this way silently never happen for conditionally rendered elements. Document that a `NodeRef` cannot be an effect dependency and change the three examples that did it: the `use_node_ref` doc example and both manual event listener examples in the events guide now use a dependency that actually changes, and read the node inside the effect. Refs yewstack#4226
|
Visit the preview URL for this PR (updated for commit c645e94): https://yew-rs--pr4257-docs-node-ref-effect-bcogw45d.web.app (expires Tue, 01 Dec 2026 04:48:04 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Benchmark - coreYew MasterPull Request |
Size ComparisonDetails
✅ None of the examples has changed their size significantly. |
Benchmark - SSRYew MasterDetails
Pull RequestDetails
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Stop documenting
NodeRefas ause_effect_withdependency, and fix the three examples that did.packages/yew/src/html/mod.rs— one paragraph onNodeRef: clones compare equal regardless of the node stored, so it cannot be an effect dependency.packages/yew/src/functional/hooks/use_ref.rs— theuse_node_refexample uses()as the dependency, and the# Tipexplains why passing the ref itself does not work.website/docs/concepts/html/events.mdx— a caution note in the manual event listener section, and both listener examples depend on()instead ofdiv_node_ref.clone().Docs only; no runtime behaviour changes.
Why
NodeRefimplementsPartialEqby identity, not by the node it currently points at:use_effect_withre-runs only whenlhs != rhs(packages/yew/src/functional/hooks/use_effect.rs:270). Two clones of one ref are always equal, so aNodeRefdependency is a constant: an effect that ran while the element was still absent never runs again after the element mounts.The examples did exactly that, and for the listener examples it is the difference between working and not working. I checked the mechanism with a standalone program that mirrors the
PartialEqimpl:That matches what #4226 reports: with a conditionally rendered element, the
use_effect_with(node_ref, ...)pattern never attaches the listener. The two listener examples are always-rendered elements, which is why the()dependency is enough for them; the note tells readers what to do when it is not.Verification
cargo test -p website-test— 133 passed, 0 failed, 25 ignored. All nineevents.mdxblocks pass, including the two edited ones (lines 388 and 454).cargo test -p yew --doc --all-features(the CI invocation iscargo test -p yew --all-features) — 37 passed, 0 failed, 1 ignored, includinguse_node_ref(line 140) andhtml::NodeRef(line 59).cargo test -p yew --all-features— 58 + 4 passed, 0 failed.cargo clippy -p yew --all-features— no warnings or errors.cargo fmt --all --check— clean.npx prettier --config website/.prettierrc.json --check website/docs/concepts/html/events.mdx— clean.The five
unexpected character '='rustdoc warnings incargo test -p website-testare pre-existing: they come from the markdown tables in five.mdxfiles and appear identically with the changes stashed.I could not run the wasm browser suite to pin the runtime behaviour in a test here:
wasm-bindgen-test-runnerstarts ChromeDriver and then fails withhttp status: 404on this machine, and that suite is not part of the change.Refs #4226