-
Notifications
You must be signed in to change notification settings - Fork 435
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
fix: replace unsafe useMemo
with useState
#8047
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
No changes to documentation |
⚡️ Editor Performance ReportUpdated Wed, 18 Dec 2024 09:23:50 GMT
Detailed information🏠 Reference resultThe performance result of
🧪 Experiment resultThe performance result of this branch
📚 Glossary
|
Component Testing Report Updated Dec 18, 2024 9:22 AM (UTC) ✅ All Tests Passed -- expand for details
|
9d075f4
to
f5cd7df
Compare
c031487
to
9b10728
Compare
useMemo
with useState
9b10728
to
29b20e6
Compare
29b20e6
to
6e3e7ab
Compare
6e3e7ab
to
e8b100a
Compare
e8b100a
to
5b55526
Compare
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.
Oh wow, this is fantastic @stipsan
I was honestly not aware of this slight gotcha with useMemo and have naturally to reached for useMemo in many of these cases.
I suppose mistakenly using useMemo for such cases is something that can easlily happen again in the future – is this something that could be detected by a linter?
It's a bit tricky, because it all comes down to wether or not it's being used as a performance optimization, or for logic. For example, in this case: import {MyIcon} from './MyIcon'
export default function Component() {
const icon = useMemo(() => <MyIcon />, [])
return (
<>
{icon}
</>
)
} In this case, if I edit |
* next: (68 commits) fix(deps): update dependency @sanity/icons to ^3.5.5 (#8105) fix(deps): update dependency @sanity/ui to ^2.10.12 (#8108) fix(deps): update dependency react-rx to ^4.1.10 (#8109) chore(deps): update dependency @sanity/tsdoc to v1.0.153 (#8107) chore(deps): update typescript-tooling (#8104) fix(deps): update dependency @sanity/icons to ^3.5.5 (#8106) feat(typegen): add support for astro (#8098) chore(deps): update dependency turbo to ^2.3.3 (#8099) fix(deps): Update dev-non-major (#8100) fix: `WebSocket is closed before the connection is established` warning (#8042) v3.68.1 fix(deps): update dependency @sanity/presentation to v1.19.13 (#8102) v3.68.0 fix: use consistent `framer-motion` semver range (#8094) refactor(core): replace `PortableTextEditor` with `EditorProvider` (#8040) fix: improve `SanityDefaultPreview` memoization (#8049) fix: tooltip position in announcements popup (#8092) fix: replace useMemo with useState (#8095) fix: replace unsafe `useMemo` with `useState` (#8047) fix: replace `React.createElement` with jsx runtime (#8043) ...
Description
From the React docs on
useMemo
:We do have some cases where the code is relying on
useMemo
never throwing away its value, and for the memo function itself to only be executed once, since the dependencies array is empty.In this PR I've refactored all of them to
useState
, as per the recommendations.By doing so we remove subtle bugs from
sanity dev
and hot module reloading, make tests less flaky, and allow React Compiler to better optimize the code since it treatsuseMemo
as purely a performance optimization that it should be safe to change. WhileuseState
is handled differently.For example, compare the before:
useMemo
may be removed and it's called more frequently than beforeuseMemo
is empty or not, it's based on what's using it.After, with
useState
it works exactly like before:What to review
Does this make sense?
Testing
Existing tests should be enough.
Notes for release
N/A – it's the type of fix where lots of tiny things feel slightly faster and more stable than before, but due to the nature of how
useMemo
behaves, and exactly how it can throw away cache if a component suspends during the initial mount, it's difficult to reproduce the issue and thus point to the fix.