Commit 933cd09
authored
feat: implement safe state management for notification hooks (MetaMask#40401)
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->
## **Description**
Root cause: whenever any team performs async local state updates and the
component unmounts, you will receive this react error (this does
eventually get cleaned up and isn't a breaking error).
E.g.
1. Component Mounts
2. Component calls an async function which once finished will update its
local state (useState)
3. Component unmounts
4. Async function finished, and tries calling setState (but component
was unmounted!) -- react error is logged (not breaking and eventually
cleaned).
This update introduces a new `useSafeState` hook to manage state updates
safely while components are mounted, preventing potential memory leaks
and reduced the opportunity for `React: State updates on unmounted
components` errors.
The `useListNotifications`, `useCreateNotifications`,
`useEnableNotifications`, and `useDisableNotifications` hooks have been
refactored to utilize `useSafeState` for loading, error, and
notifications data states. This change enhances the stability and
reliability of notifications handling in the application.
If this hook is useful, we can move this for other teams to use.
[](https://codespaces.new/MetaMask/metamask-extension/pull/40401?quickstart=1)
## **Changelog**
<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`
If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`
(This helps the Release Engineer do their job more quickly and
accurately)
-->
CHANGELOG entry: feat: implement safe state management for notification
hooks
## **Related issues**
Fixes: https://consensys.slack.com/archives/CTQAGKY5V/p1771942084169109
https://github.com/MetaMask/metamask-extension/actions/runs/22353733972/job/64687641620?pr=40327
## **Manual testing steps**
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!-- [screenshots/recordings] -->
### **After**
<!-- [screenshots/recordings] -->
## **Pre-merge author checklist**
- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **Pre-merge reviewer checklist**
- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Small, localized React hook change that only affects how async
notification actions update local state; minimal behavioral impact
beyond preventing post-unmount state updates.
>
> **Overview**
> Adds an internal `useSafeState` hook in `useNotifications.ts` that
gates `setState` calls behind a mounted check to avoid React
warnings/memory leaks when async work completes after unmount.
>
> Refactors `useListNotifications`, `useCreateNotifications`,
`useEnableNotifications`, and `useDisableNotifications` to use
`useSafeState` for `loading`/`error`/`notificationsData`, and updates
`useCallback` dependency arrays accordingly.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
2b153c2. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 4c2c0cc commit 933cd09
1 file changed
+46
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
2 | 9 | | |
3 | 10 | | |
4 | 11 | | |
| |||
18 | 25 | | |
19 | 26 | | |
20 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
21 | 56 | | |
22 | 57 | | |
23 | 58 | | |
| |||
44 | 79 | | |
45 | 80 | | |
46 | 81 | | |
47 | | - | |
48 | | - | |
49 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
50 | 85 | | |
51 | 86 | | |
52 | 87 | | |
| |||
72 | 107 | | |
73 | 108 | | |
74 | 109 | | |
75 | | - | |
| 110 | + | |
76 | 111 | | |
77 | 112 | | |
78 | 113 | | |
| |||
93 | 128 | | |
94 | 129 | | |
95 | 130 | | |
96 | | - | |
| 131 | + | |
97 | 132 | | |
98 | 133 | | |
99 | 134 | | |
| |||
106 | 141 | | |
107 | 142 | | |
108 | 143 | | |
109 | | - | |
| 144 | + | |
110 | 145 | | |
111 | 146 | | |
112 | 147 | | |
| |||
130 | 165 | | |
131 | 166 | | |
132 | 167 | | |
133 | | - | |
| 168 | + | |
134 | 169 | | |
135 | 170 | | |
136 | 171 | | |
| |||
143 | 178 | | |
144 | 179 | | |
145 | 180 | | |
146 | | - | |
| 181 | + | |
147 | 182 | | |
148 | 183 | | |
149 | 184 | | |
| |||
162 | 197 | | |
163 | 198 | | |
164 | 199 | | |
165 | | - | |
166 | | - | |
| 200 | + | |
167 | 201 | | |
168 | 202 | | |
169 | 203 | | |
| |||
176 | 210 | | |
177 | 211 | | |
178 | 212 | | |
179 | | - | |
| 213 | + | |
180 | 214 | | |
181 | 215 | | |
182 | 216 | | |
| |||
0 commit comments