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({
1414export const AlertProvider = ( { children } ) => {
1515 const [ text , setText ] = React . useState ( "" ) ;
1616 const [ type , setType ] = React . useState ( "" ) ;
17+ const timerRef = React . useRef ( null ) ;
1718
1819 const setAlert = ( { type, text } ) => {
1920 setType ( type ) ;
2021 setText ( text ) ;
2122
23+ if ( timerRef . current ) {
24+ clearTimeout ( timerRef . current ) ;
25+ }
26+
2227 if ( type !== "error" ) {
23- setTimeout ( ( ) => {
28+ timerRef . current = setTimeout ( ( ) => {
2429 setText ( "" ) ;
2530 setType ( "" ) ;
2631 } , ALERT_TIME ) ;
2732 }
2833 } ;
2934
3035 const clearAlert = ( ) => {
36+ if ( timerRef . current ) {
37+ clearTimeout ( timerRef . current ) ;
38+ }
39+
3140 setType ( "" ) ;
3241 setText ( "" ) ;
3342 }
You can’t perform that action at this time.
0 commit comments