yewstack / yew

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

use_effect lacks documentation #3563

Open ydirson opened 11 months ago

ydirson commented 11 months ago

This is about: Undocumented code

Problem

use_effect is introduced in the tutorial and described in the API doc, with a new example.

About its role the doc says "used for hooking into the component’s lifecycle", which is very vague: what does this sentence add to "called every time after the component’s render has finished"?

The doc only says "The callback is called every time after the component’s render has finished."

OTOH we see that in both examples the callback itself returns a callback, but nothing is said about this callback and when it gets called. Only a "tip" saying "The callback can return [()] if there is no destructor to run." hints that there it is likely a destructor (intended for what?). The examples themselves do not make it any more clear.

ranile commented 11 months ago

Do you have any ideas on what the documentation should be? I would be happy to look at any PRs updating the docs

ydirson commented 10 months ago

Do you have any ideas on what the documentation should be? I would be happy to look at any PRs updating the docs

I would be happy to reach the level of understanding where I can submit doc PRs, but as you can see from this ticket I will need help to get there, and I expected answers to my questions would be part of that. What follows come from the small level of insights I could get, and as you see it brings additional questions.

With a bit of step back, but without having still assimilated the whole stuff, my impression a lot of terminology about hooks is confusing, for example:

From what I gathered, I'd think that a few renamings could help newcomers greatly, eg.:

Another aspect that would also need more details is the use of "dependencies": many examples show () as dependency to prevent any change of the impacted state, and the fact is uses a generic type makes it far from obvious what else can be used. Would it be that it could just be any value, which would stored as state and compared to potentially trigger the nominal behavior?

In the case of use_effect, which IIUC does not provide any state otherwise, it seems that use_effect_with would be the thing that blurs the line between what is a state variable and what is a hook. Are there other such examples, that would not fall into only one of those categories?

Oh, and only when reading the example of use_node_ref do I get an idea that the dependency value gets passed to the callback we give to use_effect_with, this kind of things ought to be documented explicitly.