Callbacks should be made within the lifetime of the component's scope
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
The issue names functional components, yew_functional::use_state, useCallback, hooks, and component scope; begin by tracing those APIs and how their lifetimes are handled. Done means callbacks can borrow hook data for the component lifetime without requiring 'static or cloning captured values, including the shown pattern.
Written by the indexing model from the issue text.
Description
Describe the feature you'd like
Currently, callbacks made within functional components are annoying to work with. The requirements on callback don't necessarily require 'static, but are pretty much 'static. Note, this means that closures can't contain references to borrowed data, meaning any values used by other state mechanisms need to be cloned into the closure.
However, this wouldn't be necessary if the callbacks could borrow data from other hooks. We can't set the lifetime to be that of the function component itself, but if the data borrowed from the hooks could share the lifetime of the scope, then we could do something along these lines:
fn test_element() {
let (dragging, set_dragging) = yew_functional::use_state(|| false);
let g = RefCell::new(dragging);
let r = &g;
let a1 = useCallback(move || println!(r.borrow())));
let a2 = useCallback(move || println!(r.borrow()));
let a3 = useCallback(move || println!(r.borrow()));
}
From there I imagine we could wrap the hooks in an interface like RefCell that also updates the hook and component.
I'm not 100% sure on where the lifetime would be coming from, since there's no lifetime provided to functional components (instead relying on thread-locals). I imagine we could get a lifetime either when the app is launched (effectively being static) or on the creation of components.
Questionnaire
- I'm interested in implementing this myself but don't know where to start
- I would like to add this feature
- I don't have time to add this right now, but maybe later
- Dominant language
- Rust
- Stars
- 32.8k
- Forks
- 1.5k
- Avg merge
- 5h 34m
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from yewstack/yew
-
documentation
Difficulty 3/5 1-2 days Newbie friendliness 70/100
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
documentation
Difficulty 4/5 3-5 days Newbie friendliness 42/100
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
enhancement remote
Difficulty 2/5 1-3 hours Newbie friendliness 68/100