-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add use_callback hook #2566
Add use_callback hook #2566
Conversation
Visit the preview URL for this PR (updated for commit e92a224): https://yew-rs--pr2566-use-callback-cm306hsq.web.app (expires Fri, 08 Apr 2022 06:46:28 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Size Comparison
|
F: Fn(IN) -> OUT + 'static, | ||
D: PartialEq + 'static, | ||
{ | ||
let callback = use_state(|| -> RefCell<Option<Callback<IN, OUT>>> { RefCell::new(None) }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I am missing something, this can simply be
(*use_memo(move |_| Callback::from(f), deps)).clone()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct, updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then I'm wondering whether we need to add use_callback at all? but React uses useCallback a lot for performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another issue, the array of dependencies is not passed as arguments to the callback, if callback wants to consume deps, that might not be convenient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let oncallback = {
let counter = counter.clone();
let counter2 = counter.clone();
use_callback(
move |e| *counter2,
counter
)
};
vs
let oncallback = {
let counter = counter.clone();
use_memo(
move |counter| {
Callback::from(move |e| **counter)
},
counter
)
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@futursolo any advice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that React has a separate useCallback
primarily due to useMemo(() => () => {}, [])
looks a little bit complicated. I think in this case, if use_callback
manages deps
for you, it might still be worth it.
So one can do something like:
use_callback(move |deps| move |in_type| {}, deps);
// or have some magic by wrapping it in a custom closure.
use_callback(|deps, in_type| {}, deps);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use_callback(|deps, in_type| {}, deps);
looks interesting, I'll have a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use use_memo
?
All i see this hook does is remove the boilerplate of writing Callback::from(/* my closure*/)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah but i guess it wont hurt...
Love the docs and tests 👍
The only thing it changes is the reduced boilerplate and Rc::new to Rc::clone on creating a callback, right? |
@hamza1311 use_memo returns |
I meant the difference between |
The difference between
React projects uses useCallback a lot, like |
Description
use_callback
hook, used for obtaining an immutable reference to a memoizedCallback
, simply a clone of useCallback of React: https://reactjs.org/docs/hooks-reference.html#usecallbackuse_memo
hookChecklist
cargo make pr-flow