Skip to content

Commit 88352d0

Browse files
authored
refactor(react): avoid eslint exception (#1176)
* fix(react): avoid eslint exception * make it symmetric
1 parent f05bc4b commit 88352d0

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/react.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,25 @@ export function useSnapshot<T extends object>(
131131
[proxyObject],
132132
)
133133
const lastSnapshot = useRef<Snapshot<T>>(undefined)
134-
let inRender = true
134+
const subscribed = useRef(false)
135135
const currSnapshot = useSyncExternalStore(
136136
useCallback(
137137
(callback) => {
138+
subscribed.current = true
138139
const unsub = subscribe(proxyObject, callback, notifyInSync)
139140
callback() // Note: do we really need this?
140-
return unsub
141+
return () => {
142+
unsub()
143+
subscribed.current = false
144+
}
141145
},
142146
[proxyObject, notifyInSync],
143147
),
144148
() => {
145149
const nextSnapshot = snapshot(proxyObject)
146150
try {
147151
if (
148-
!inRender &&
152+
subscribed.current &&
149153
lastSnapshot.current &&
150154
!isChanged(
151155
lastSnapshot.current,
@@ -164,9 +168,6 @@ export function useSnapshot<T extends object>(
164168
},
165169
() => snapshot(proxyObject),
166170
)
167-
// TODO how could we bypass this?
168-
// eslint-disable-next-line react-hooks/immutability
169-
inRender = false
170171
useLayoutEffect(() => {
171172
lastSnapshot.current = currSnapshot
172173
})

0 commit comments

Comments
 (0)