Synphonyte / leptos-use

Collection of essential Leptos utilities inspired by React-Use / VueUse / SolidJS-USE
https://leptos-use.rs/
Apache License 2.0
309 stars 67 forks source link

[Bug] on_click_outside lint message appears even though it (probably) shouldn't #118

Closed SleeplessOne1917 closed 3 months ago

SleeplessOne1917 commented 3 months ago

I'm using on_click_outside in my codebase and am getting this lint warning:

unused implementer of `FnOnce` that must be used
closures are lazy and do nothing unless called

Here is the relevant snippet of code it's complaining about:

  let dropdown_node_ref = NodeRef::<Details>::new(); // This gets used in code not included here
  on_click_outside(dropdown_node_ref, move |_| {
    // Using this approach instead of conditional rendering so that the dropdown works at least somewhat when JS is disabled
    if let Some(el) = dropdown_node_ref.get() {
      let _ = el.attr("open", None::<&str>);
    }
  });

Is this a problem with how I'm using the function, or is this an issue in the library?

maccesch commented 3 months ago

on_click_outside returns a closure that you can call to stop the behavior. So to get rid of this warning you can simply do

let _ = on_click_outside(...)
SleeplessOne1917 commented 3 months ago

Ah, my bad. Thank you for your prompt response.

maccesch commented 3 months ago

It's not documented, so guess my bad 😂

SleeplessOne1917 commented 3 months ago

Out of curiosity, what would one do with the closure returned from on_click_outside anyway?

maccesch commented 3 months ago

To "unlisten" everything