yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.84k stars 1.43k forks source link

Caution about navigator callbacks in functional components is confusing #3580

Open EndilWayfare opened 10 months ago

EndilWayfare commented 10 months ago

This is about:

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)