···1+import {useCallback, useInsertionEffect, useRef} from 'react'
2+3+// This should be used sparingly. It erases reactivity, i.e. when the inputs
4+// change, the function itself will remain the same. This means that if you
5+// use this at a higher level of your tree, and then some state you read in it
6+// changes, there is no mechanism for anything below in the tree to "react"
7+// to this change (e.g. by knowing to call your function again).
8+//
9+// Also, you should avoid calling the returned function during rendering
10+// since the values captured by it are going to lag behind.
11+export function useNonReactiveCallback<T extends Function>(fn: T): T {
12+ const ref = useRef(fn)
13+ useInsertionEffect(() => {
14+ ref.current = fn
15+ }, [fn])
16+ return useCallback(
17+ (...args: any) => {
18+ const latestFn = ref.current
19+ return latestFn(...args)
20+ },
21+ [ref],
22+ ) as unknown as T
23+}
+10-6
src/view/com/modals/Modal.tsx
···9import once from 'lodash.once'
1011import {useModals, useModalControls} from '#/state/modals'
012import * as ConfirmModal from './Confirm'
13import * as EditProfileModal from './EditProfile'
14import * as ProfilePreviewModal from './ProfilePreview'
···5051 const navigateOnce = once(navigate)
5253- const onBottomSheetAnimate = (_fromIndex: number, toIndex: number) => {
54- if (activeModal?.name === 'profile-preview' && toIndex === 1) {
55- // begin loading the profile screen behind the scenes
56- navigateOnce('Profile', {name: activeModal.did})
57- }
58- }
00059 const onBottomSheetChange = async (snapPoint: number) => {
60 if (snapPoint === -1) {
61 closeModal()
···9import once from 'lodash.once'
1011import {useModals, useModalControls} from '#/state/modals'
12+import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
13import * as ConfirmModal from './Confirm'
14import * as EditProfileModal from './EditProfile'
15import * as ProfilePreviewModal from './ProfilePreview'
···5152 const navigateOnce = once(navigate)
5354+ // It seems like the bottom sheet bugs out when this callback changes.
55+ const onBottomSheetAnimate = useNonReactiveCallback(
56+ (_fromIndex: number, toIndex: number) => {
57+ if (activeModal?.name === 'profile-preview' && toIndex === 1) {
58+ // begin loading the profile screen behind the scenes
59+ navigateOnce('Profile', {name: activeModal.did})
60+ }
61+ },
62+ )
63 const onBottomSheetChange = async (snapPoint: number) => {
64 if (snapPoint === -1) {
65 closeModal()