yew icon indicating copy to clipboard operation
yew copied to clipboard

Caution about navigator callbacks in functional components is confusing

Open EndilWayfare opened this issue 2 years ago • 0 comments

This is about:

  • [ ] A typo
  • [x] Innaccurate/misleading documentation (e.g. technically incorrect advice)
  • [ ] Undocumented code
  • [x] Outdated documentation
  • [ ] Other

Problem

https://yew.rs/docs/concepts/router#function-components

The example here uses Callback::from. Use a normal callback if the target route can be the same as the route the component is in, or just to play safe.

What is a "normal" Callback in this context? The quoted wording of the Caution suggests that it's something different than what's returned from Callback::from. But that's the normal-est Callback I can think of.

The Caution goes on to warn about a panic! that would be caused by a button on a page that goes back to the same page. I understand why the hook would not trigger a re-render in that case, but I don't understand why that would panic. I copy/pasted the code into a quick example, and it worked just fine.

Is this perhaps a vestige of CallbackOnce? That's the only thing I can imagine that would cause a panic, if you tried to call a FnOnce twice.

Details about the solution you'd like (Optional)

If this is out of date, and the caveat no longer applies, just removing the Caution should be sufficient.

If there still is something to watch out for, the wording should more explicitly describe the "good" and "bad" option and why the "bad" one will panic, ideally with example code that can be verified to, in fact, panic.

Additional context (Optional)

Questionaire (Optional)

  • [ ] I'd like to write this documentation
  • [x] 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

EndilWayfare avatar Jan 05 '24 02:01 EndilWayfare