File tree Expand file tree Collapse file tree 1 file changed +10
-1
lines changed Expand file tree Collapse file tree 1 file changed +10
-1
lines changed Original file line number Diff line number Diff line change @@ -14,20 +14,29 @@ const AlertContext = React.createContext({
14
14
export const AlertProvider = ( { children } ) => {
15
15
const [ text , setText ] = React . useState ( "" ) ;
16
16
const [ type , setType ] = React . useState ( "" ) ;
17
+ const timerRef = React . useRef ( null ) ;
17
18
18
19
const setAlert = ( { type, text } ) => {
19
20
setType ( type ) ;
20
21
setText ( text ) ;
21
22
23
+ if ( timerRef . current ) {
24
+ clearTimeout ( timerRef . current ) ;
25
+ }
26
+
22
27
if ( type !== "error" ) {
23
- setTimeout ( ( ) => {
28
+ timerRef . current = setTimeout ( ( ) => {
24
29
setText ( "" ) ;
25
30
setType ( "" ) ;
26
31
} , ALERT_TIME ) ;
27
32
}
28
33
} ;
29
34
30
35
const clearAlert = ( ) => {
36
+ if ( timerRef . current ) {
37
+ clearTimeout ( timerRef . current ) ;
38
+ }
39
+
31
40
setType ( "" ) ;
32
41
setText ( "" ) ;
33
42
}
You can’t perform that action at this time.
0 commit comments