`NodeRef` documentation is misleading about usage with `use_effect_with`

Open
#4,226 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
70/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
documentation

Research direction

Start with the use_node_ref documentation in packages/yew/src/functional/hooks/use_ref.rs and the manual event listener examples in website/docs/concepts/html/events.mdx. Review how these examples describe passing a NodeRef to use_effect_with, then update the guidance so the documented behavior and limitations are clear and the examples no longer mislead users.

Written by the indexing model from the issue text.

Description

documentation

This is about:

  • A typo
  • Inaccurate/misleading documentation (e.g. technically incorrect advice)
  • Undocumented code
  • Outdated documentation
  • Other

Problem

This is a follow up to #4225. I closed that as a duplicate because I realized that the NodeRef behavior is intentional and other people have brought it up before, but after looking at the documentation more I think I was accidentally misled several times and that's what led to my misunderstanding.

Examples:

The docs for use_node_ref pass the node ref into use_effect_with:

https://github.com/yewstack/yew/blob/bfa6c19af971084f9547495388bde9aceeb81aaa/packages/yew/src/functional/hooks/use_ref.rs#L148-L153

There is even a tip that says:

https://github.com/yewstack/yew/blob/bfa6c19af971084f9547495388bde9aceeb81aaa/packages/yew/src/functional/hooks/use_ref.rs#L183-L187

This is misleading and is the exact example I used in #4225. If you conditionally render an element, its NodeRef::get method will return None until it is rendered the first time. Since NodeRef uses pointer equality for PartialEq, the effect will never run again after that even when NodeRef is eventually Some because the node was rendered.

The specific events example from #4225 and the buggy use_event hook in yew-hooks that I cite in that issue are both essentially just cleaned up versions of the "Manual event listener" examples from the docs.

From "Using Closure (verbose)":

https://github.com/yewstack/yew/blob/bfa6c19af971084f9547495388bde9aceeb81aaa/website/docs/concepts/html/events.mdx?plain=1#L387-L390

From "Using gloo (concise)":

https://github.com/yewstack/yew/blob/bfa6c19af971084f9547495388bde9aceeb81aaa/website/docs/concepts/html/events.mdx?plain=1#L454-L457

Both of these also pass a NodeRef into use_effect_with which will only work in certain very simple cases.

Questionaire (Optional)

  • I'd like to write this documentation
  • I'd like to write this documentation but I'm not sure what's needed
  • I don't have time to add this right now, but maybe later

I'm happy to help resolve this and update the docs, but I'd appreciate some guidance for how you'd like me to word the changes. I'm just hoping to fix this so future people (including my future forgetful self) don't make the same mistakes. This is a subtle footgun that is easy to mess up if you're not very careful.

Dominant language
Rust
Stars
32.8k
Forks
1.5k
Avg merge
5h 34m
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from yewstack/yew

All issues in yewstack/yew

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.