File tree 3 files changed +33
-5
lines changed 3 files changed +33
-5
lines changed Original file line number Diff line number Diff line change 7
7
CI_JOB_NUMBER : 1
8
8
steps :
9
9
- uses : actions/checkout@v2
10
- - uses : pnpm/action-setup@v2.2.2
10
+ - uses : pnpm/action-setup@v4
11
11
with :
12
- version : 7
12
+ version : 9
13
13
- uses : andresz1/size-limit-action@v1
14
14
with :
15
15
github_token : ${{ secrets.GITHUB_TOKEN }}
Original file line number Diff line number Diff line change 1
- import { useEffect , useState } from 'react' ;
1
+ import { useEffect , useState , useRef } from 'react' ;
2
2
import { DefaultToastOptions , Toast , ToastType } from './types' ;
3
3
4
4
const TOAST_LIMIT = 20 ;
@@ -143,15 +143,21 @@ export const defaultTimeouts: {
143
143
144
144
export const useStore = ( toastOptions : DefaultToastOptions = { } ) : State => {
145
145
const [ state , setState ] = useState < State > ( memoryState ) ;
146
+ const initial = useRef ( memoryState ) ;
147
+
148
+ // TODO: Switch to useSyncExternalStore when targeting React 18+
146
149
useEffect ( ( ) => {
150
+ if ( initial . current !== memoryState ) {
151
+ setState ( memoryState ) ;
152
+ }
147
153
listeners . push ( setState ) ;
148
154
return ( ) => {
149
155
const index = listeners . indexOf ( setState ) ;
150
156
if ( index > - 1 ) {
151
157
listeners . splice ( index , 1 ) ;
152
158
}
153
159
} ;
154
- } , [ state ] ) ;
160
+ } , [ ] ) ;
155
161
156
162
const mergedToasts = state . toasts . map ( ( t ) => ( {
157
163
...toastOptions ,
Original file line number Diff line number Diff line change 1
- import React from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
import {
3
3
render ,
4
4
screen ,
@@ -319,3 +319,25 @@ test('pause toast', async () => {
319
319
320
320
expect ( toastElement ) . not . toBeInTheDocument ( ) ;
321
321
} ) ;
322
+
323
+ test ( '"toast" can be called from useEffect hook' , async ( ) => {
324
+ const MyComponent = ( ) => {
325
+ const [ success , setSuccess ] = useState ( false ) ;
326
+ useEffect ( ( ) => {
327
+ toast . success ( 'Success toast' ) ;
328
+ setSuccess ( true ) ;
329
+ } , [ ] ) ;
330
+
331
+ return success ? < div > MyComponent finished</ div > : null ;
332
+ } ;
333
+
334
+ render (
335
+ < >
336
+ < MyComponent />
337
+ < Toaster />
338
+ </ >
339
+ ) ;
340
+
341
+ await screen . findByText ( / M y C o m p o n e n t f i n i s h e d / i) ;
342
+ expect ( screen . queryByText ( / S u c c e s s t o a s t / i) ) . toBeInTheDocument ( ) ;
343
+ } ) ;
You can’t perform that action at this time.
0 commit comments