Skip to content

docs: stop recommending a NodeRef as a use_effect_with dependency - #4257

Open
Goldloli wants to merge 1 commit into
yewstack:masterfrom
Goldloli:docs/node-ref-effect-deps
Open

Goldloli wants to merge 1 commit into
yewstack:masterfrom
Goldloli:docs/node-ref-effect-deps

Conversation

@Goldloli

Copy link
Copy Markdown

What

Stop documenting NodeRef as a use_effect_with dependency, and fix the three examples that did.

  • packages/yew/src/html/mod.rs — one paragraph on NodeRef: clones compare equal regardless of the node stored, so it cannot be an effect dependency.
  • packages/yew/src/functional/hooks/use_ref.rs — the use_node_ref example uses () as the dependency, and the # Tip explains 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 of div_node_ref.clone().

Docs only; no runtime behaviour changes.

Why

NodeRef implements PartialEq by identity, not by the node it currently points at:

// packages/yew/src/html/mod.rs:93-97
impl PartialEq for NodeRef {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(self.0.as_ptr(), other.0.as_ptr())
    }
}

use_effect_with re-runs only when lhs != rhs (packages/yew/src/functional/hooks/use_effect.rs:270). Two clones of one ref are always equal, so a NodeRef dependency 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 PartialEq impl:

node observed when the effect ran: None
node available now:                Some(42)
effect re-runs on the next render: false
deps carrying the node value change: true

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 nine events.mdx blocks pass, including the two edited ones (lines 388 and 454).
  • cargo test -p yew --doc --all-features (the CI invocation is cargo test -p yew --all-features) — 37 passed, 0 failed, 1 ignored, including use_node_ref (line 140) and html::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 in cargo test -p website-test are pre-existing: they come from the markdown tables in five .mdx files 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-runner starts ChromeDriver and then fails with http status: 404 on this machine, and that suite is not part of the change.

Refs #4226

`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
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

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 🌎

@github-actions

Copy link
Copy Markdown

Benchmark - core

Yew Master

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.403 ns      │ 2.565 ns      │ 2.409 ns      │ 2.413 ns      │ 100     │ 1000000000

Pull Request

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.434 ns      │ 3.684 ns      │ 2.44 ns       │ 2.487 ns      │ 100     │ 1000000000

@github-actions

Copy link
Copy Markdown

Size Comparison

Details
examples master (KB) pull request (KB) diff (KB) diff (%)
async_clock 100.039 100.039 0 0.000%
boids 164.866 164.866 0 0.000%
communication_child_to_parent 93.571 93.571 0 0.000%
communication_grandchild_with_grandparent 105.853 105.853 0 0.000%
communication_grandparent_to_grandchild 102.182 102.182 0 0.000%
communication_parent_to_child 90.968 90.968 0 0.000%
contexts 106.163 106.163 0 0.000%
counter 86.113 86.113 0 0.000%
counter_functional 88.155 88.155 0 0.000%
dyn_create_destroy_apps 90.023 90.023 0 0.000%
file_upload 98.432 98.432 0 0.000%
function_delayed_input 94.491 94.491 0 0.000%
function_memory_game 168.570 168.570 0 0.000%
function_router 401.683 401.683 0 0.000%
function_todomvc 164.875 164.875 0 0.000%
futures 237.143 237.143 0 0.000%
game_of_life 100.482 100.482 0 0.000%
immutable 259.346 259.346 0 0.000%
inner_html 80.654 80.654 0 0.000%
js_callback 109.636 109.636 0 0.000%
keyed_list 176.187 176.187 0 0.000%
mount_point 84.136 84.136 0 0.000%
nested_list 112.575 112.575 0 0.000%
node_refs 91.643 91.643 0 0.000%
password_strength 1772.990 1772.990 0 0.000%
portals 93.450 93.450 0 0.000%
router 367.893 367.893 0 0.000%
suspense 113.827 113.827 0 0.000%
timer 88.385 88.385 0 0.000%
timer_functional 99.277 99.277 0 0.000%
todomvc 141.896 141.896 0 0.000%
two_apps 86.055 86.055 0 0.000%
web_worker_fib 136.117 136.117 0 0.000%
web_worker_prime 186.438 186.438 0 0.000%
webgl 82.784 82.784 0 0.000%

✅ None of the examples has changed their size significantly.

@github-actions

Copy link
Copy Markdown

Benchmark - SSR

Yew Master

Details
Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 310.433 312.536 311.158 0.662
Hello World 10 469.167 476.901 473.599 2.309
Function Router 10 32480.772 32620.082 32559.490 46.191
Concurrent Task 10 1005.924 1007.363 1006.923 0.445
Many Providers 10 1034.650 1070.798 1054.789 12.022

Pull Request

Details
Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 310.677 318.972 311.875 2.520
Hello World 10 447.107 474.558 453.751 8.557
Function Router 10 32357.634 32823.903 32617.325 142.015
Concurrent Task 10 1006.210 1007.419 1006.958 0.379
Many Providers 10 1009.720 1128.175 1049.516 37.279

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant