Skip to content

Commit 4f30194

Browse files
committed
492: Add timerRef to not clear successive success banners early
1 parent fc35c54 commit 4f30194

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/client/src/contexts/AlertContext.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,29 @@ const AlertContext = React.createContext({
1414
export 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
}

0 commit comments

Comments
 (0)