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

added useMemo instead of useState #5183

Closed
wants to merge 2 commits into from

Conversation

jerryseigle
Copy link

In most cases, you don't need to update Uppy via state. It's better to use useMemo to ensure that Uppy is initialized only once.

In most cases, you don't need to update Uppy via state. It's better to use useMemo to ensure that Uppy is initialized only once.
Copy link
Contributor

Diff output files
No diff

@aduh95 aduh95 requested a review from Murderlon May 22, 2024 11:41
Copy link
Member

@Murderlon Murderlon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we went with this because we only want it be initialised once and useMemo can't guarantee that:

React will not throw away the cached value unless there is a specific reason to do that. For example, in development, React throws away the cache when you edit the file of your component. Both in development and in production, React will throw away the cache if your component suspends during the initial mount. In the future, React may add more features that take advantage of throwing away the cache—for example, if React adds built-in support for virtualized lists in the future, it would make sense to throw away the cache for items that scroll out of the virtualized table viewport. This should be fine if you rely on useMemo solely as a performance optimization. Otherwise, a state variable or a ref may be more appropriate.
React docs

That left us with two options, useRef or useState (with initialiser function). The problem with refs is that reading them during the render is bad practise. Even a former React core team member chipped in on Uppy+Refs.

So that's where we're at with useState.

@Murderlon
Copy link
Member

In hindsight, I regret deprecating and removing useUppy instead of just putting the awkward useState + initialiser function in there. It would be a tiny hook, barely doing anything, but at least it would confuse people less. Maybe it will make a comeback under a different name, but for now I think useMemo is not what we want.

@Murderlon Murderlon closed this May 27, 2024
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

Successfully merging this pull request may close these issues.

None yet

2 participants