Where can I and can't I use hooks? #4045
-
|
While playing around with the code of the tutorial, I discovered that I can't move a hook to a separate function without changing the behaviour of the program. Let me show the adaptations I made to the tutorial. First, some refactoring that works: But if I try to move the use_effect_with to a separate function, I end up with an empty list of videos. I had a look at the Rules of hooks, but could not figure out that those rules forbid this. And I get no compile or runtime error. So, why does moving that code to the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I agree this is a bit misleading. alternatively you can make this work by making load_videos a custom hook with the "hook" attribute: #[hook]
fn use_load_videos(videos: UseStateHandle<Vec<Video>>) {
use_effect_with((), move |_| {
set_videos(videos);
|| ()
});
} |
Beta Was this translation helpful? Give feedback.
-
|
On second thought, this is valuable feedback. Normally when users place a hook wrong it doesn't compile. But I realize now these hooks with no return value can be go under the radar. Yew can definitely use some improvements on catching these outliers. |
Beta Was this translation helpful? Give feedback.
I agree this is a bit misleading.
All
functions above actually mean "function component"alternatively you can make this work by making load_videos a custom hook with the "hook" attribute: