yewstack / yewstack/yew

Correct way to handle `NodeRef` reassignments?

Open
#1,439 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Question

I'm using refs to register event listeners for non-Yew-supported event types. Following the node_refs example, it makes sense to use the rendered method to do setup on first render. But what happens when the value of the ref changes? E.g. if the Node being refd is rendered conditionally? In React, this is where I would reach for a "callback ref" passed a class method or a function with a bound this. Maybe the Yew analog would be a Callback to dispatch a message upon ref assignment?

What I've tried

I can store the result of ref.get() in the component struct and do an equality check in rendered, but this strikes me as roundabout and not incredibly ergonomic. It's possible that not many people have (or have expressed) this use case, and thus it's not even on yew's radar. That's perfectly alright. I'm just wondering if there's already a "blessed" strategy for this that I'm simply overlooking.

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.

Research direction

Start with the node_refs example and the component's rendered lifecycle, both referenced in the issue. Determine how NodeRef behaves when its target is conditionally reassigned and whether an existing callback or message pattern covers that case. Done means documenting the supported strategy or clearly scoping the API change required.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
frontend, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.