@@ -11,12 +11,11 @@ import type {
1111import React , {
1212 createContext ,
1313 createElement ,
14- useCallback ,
1514 useContext ,
16- useMemo ,
1715 useRef ,
1816 useState ,
1917} from 'react'
18+ import { useStateToRef } from 'react-shortcut-guide'
2019
2120import { useIsClient } from '~/hooks/use-is-client'
2221import { useStore } from '~/store'
@@ -98,6 +97,8 @@ export const ModalStackProvider: FC<{
9897} > = observer ( ( props ) => {
9998 const { children } = props
10099 const [ modalStack , setModalStack ] = useState < IModalStackStateType [ ] > ( [ ] )
100+ const modalStackRef = useStateToRef ( modalStack )
101+
101102 const [ extraModalPropsMap , setExtraModalPropsMap ] = useState <
102103 Map <
103104 string ,
@@ -115,7 +116,7 @@ export const ModalStackProvider: FC<{
115116 new WeakMap < FunctionComponentElement < any > , ( ) => any > ( ) ,
116117 )
117118
118- const present = useCallback ( ( comp : IModalStackComponent ) : Disposer => {
119+ const present = useRef ( ( comp : IModalStackComponent ) : Disposer => {
119120 const {
120121 component,
121122 props,
@@ -143,24 +144,6 @@ export const ModalStackProvider: FC<{
143144 return ( ) => null
144145 }
145146
146- const $modalElement : FunctionComponentElement < any > = createElement (
147- Modal ,
148- {
149- ...modalProps ,
150- modalId : id ,
151- useBottomDrawerInMobile,
152- key : id ,
153- ref : ( ins ) => {
154- modalRefMap . current . set ( $modalElement , ins ! )
155- } ,
156- disposer : ( ) => {
157- dismissFnMapRef . current . delete ( $modalElement )
158- setModalStack ( ( prev ) => prev . filter ( ( item ) => item . id !== id ) )
159- } ,
160- } ,
161- modalChildren ,
162- )
163-
164147 const disposer = ( immediately = false ) => {
165148 const immediatelyDisposer = ( ) => {
166149 setModalStack ( ( stack ) => {
@@ -179,6 +162,21 @@ export const ModalStackProvider: FC<{
179162 }
180163 }
181164
165+ const $modalElement : FunctionComponentElement < any > = createElement (
166+ Modal ,
167+ {
168+ ...modalProps ,
169+ modalId : id ,
170+ useBottomDrawerInMobile,
171+ key : id ,
172+ ref : ( ins ) => {
173+ modalRefMap . current . set ( $modalElement , ins ! )
174+ } ,
175+ disposer,
176+ } ,
177+ modalChildren ,
178+ )
179+
182180 setModalStack ( ( stack ) => {
183181 return [
184182 ...stack ,
@@ -198,39 +196,36 @@ export const ModalStackProvider: FC<{
198196 return new Map ( map )
199197 } )
200198 return disposer
201- } , [ ] )
199+ } ) . current
202200
203- const findCurrentByName = useCallback (
204- ( name : string ) => {
205- return modalStack . find ( ( item ) => item . name === name || item . id === name )
206- } ,
207- [ modalStack ] ,
208- )
201+ const findCurrentByName = useRef ( ( name : string ) => {
202+ const modalStack = modalStackRef . current
203+ return modalStack . find ( ( item ) => item . name === name || item . id === name )
204+ } ) . current
209205
210- const getStack = useCallback ( ( ) => {
206+ const getStack = useRef ( ( ) => {
207+ const modalStack = modalStackRef . current
211208 return modalStack . concat ( )
212- } , [ modalStack ] )
209+ } ) . current
213210
214- const disposeAll = useCallback (
215- async ( immediately = false ) => {
216- const reversedStack = modalStack . concat ( ) . reverse ( )
217- if ( immediately ) {
218- reversedStack . forEach ( ( current ) => current . disposer ( ) )
219- } else {
220- for ( const current of reversedStack ) {
221- const instance = modalRefMap . current . get ( current . component )
211+ const disposeAll = useRef ( async ( immediately = false ) => {
212+ const modalStack = modalStackRef . current
213+ const reversedStack = modalStack . concat ( ) . reverse ( )
214+ if ( immediately ) {
215+ reversedStack . forEach ( ( current ) => current . disposer ( ) )
216+ } else {
217+ for ( const current of reversedStack ) {
218+ const instance = modalRefMap . current . get ( current . component )
222219
223- if ( ! instance ) {
224- current . disposer ( )
225- continue
226- }
227- await instance . dismiss ( )
220+ if ( ! instance ) {
228221 current . disposer ( )
222+ continue
229223 }
224+ await instance . dismiss ( )
225+ current . disposer ( )
230226 }
231- } ,
232- [ modalStack ] ,
233- )
227+ }
228+ } ) . current
234229
235230 const isClient = useIsClient ( )
236231
@@ -243,10 +238,9 @@ export const ModalStackProvider: FC<{
243238
244239 return (
245240 < ModalStackContext . Provider
246- value = { useMemo (
247- ( ) => ( { present, findCurrentByName, getStack, disposeAll } ) ,
248- [ disposeAll , findCurrentByName , getStack , present ] ,
249- ) }
241+ value = {
242+ useRef ( { present, findCurrentByName, getStack, disposeAll } ) . current
243+ }
250244 >
251245 { children }
252246
0 commit comments