Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callbacks should be made within the lifetime of the component's scope #1656

Open
1 of 3 tasks
jkelleyrtp opened this issue Nov 29, 2020 · 1 comment
Open
1 of 3 tasks
Labels
A-yew Area: The main yew crate feature-request A feature request

Comments

@jkelleyrtp
Copy link
Contributor

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
@jkelleyrtp jkelleyrtp added the feature-request A feature request label Nov 29, 2020
@ranile
Copy link
Member

ranile commented Nov 29, 2020

I think the lifetime should be from component being rendered to component being destroyed (re-render would call the hooks again.) I'm not sure if that is possible though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew Area: The main yew crate feature-request A feature request
Projects
None yet
Development

No branches or pull requests

3 participants