Skip to content
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

useMemo not explained in Tutorial #1025

Open
MightyPork opened this issue Nov 14, 2023 · 5 comments
Open

useMemo not explained in Tutorial #1025

MightyPork opened this issue Nov 14, 2023 · 5 comments

Comments

@MightyPork
Copy link

MightyPork commented Nov 14, 2023

The Context part of the tutorial uses the useMemo() hook, but there's no explanation what this does and how it's used.

Later you are asked to use it yourself when solving the exercise. I still didn't understand what it does, even after reading the solution.

@JoviDeCroock
Copy link
Member

That's valuable feedback thank you! useMemo returns a stable reference for a given value, so it can be used to run expensive computations, .... and only re-calculate when the dependencies contained within the second argument (the array) change.

@Antarian
Copy link

I understand useMemo is used for expensive computations, but somehow failing to understand why in tutorial user or increment are defined as expensive computations?
Is it just random example to show useMemo hook?

@rschristian
Copy link
Member

As Jovi mentions, memoization isn't exclusively for expensive computations, it can also used to create stable references. Without memoizing in that example, each render of <App /> would create a new auth object thereby triggering the context in all consumers even if the inner values (user and setUser) haven't changed.

It might not be the best example (contributions welcome & appreciated!) but it does serve a purpose, it's not "random".

@Antarian
Copy link

Thank you for explanation. It makes more sense to me now.
I have also checked React site for useMemo. There are more possible use cases. It's more that I am starting with Preact without a knowledge of React.

@rschristian
Copy link
Member

Fully agree, we could do with fleshing out our hooks docs in general, it's all super light on details, examples, etc.

Will go on my to-do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants