-
Notifications
You must be signed in to change notification settings - Fork 61
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
[Question] Getting several context values at once? #93
Comments
You can do that if you memoize the selector function. For example import memoize from 'proxy-memoize';
const selector = memoize(({ v1, v2, v3 }) => ({ v1, v2, v3 }));
const Component = () => {
const { v1, v2, v3 } = useContextSelector(Ctx, selector);
// ...
}; |
Thx for this quick answer! I must be missing something but, why should I use memoization? Aren't both patterns eqs? |
Because const { v1, v2, v3 } = useContextSelector(({ v1, v2, v3 }) => ({ v1, v2, v3 })) will trigger re-renders for any changes (like So, "one call, one value" pattern is the rule we follow. You might be interested in reading #19 discussion. |
Hmmm... I'm not sure to perfectly understand why, but I'll be looking for some infos within the linked discussion 👍 Thx a lot @dai-shi ! |
Assuming you are not talking about custom equality function, const { v1, v2, v3 } = useTrackedState()
|
Ok thx a lot for all these informations, let's read (and learn) then 🙂 |
Closing as answered. |
Hi 👋
First things first, thx a lot for this great work! 👏 I'm still discovering it, and maybe I'm going to ask a silly question but... is there any reason why
useContextSelector
examples were always using the same "one call, one value" pattern:...which seems quite verbose to me. Couldn't we simply use a single call such as:
Thx a lot for your answer! 🙏
The text was updated successfully, but these errors were encountered: