Skip to content

Commit 070b484

Browse files
author
Afzal
committed
chore: wraps open and close functions in useCallback hook
- current these function reference changes everytime the component renders /re-renders which causes infinite looping issues when used in side effects(useEffect), wrapping it in useCallback fixes the issue of the reference been changed rapidly
1 parent fc1a051 commit 070b484

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/@next/Alert/AlertProvider.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import React, { useState } from 'react';
1+
import React, { useCallback, useState } from 'react';
22
import { AlertProps } from './Alert';
33
import { AlertContext, ShowAlertProps } from './AlertContext';
44

55
export const AlertProvider = ({ children }: { children: React.ReactNode }) => {
66
const [state, setState] = useState<AlertProps>({ show: false });
77

8-
const open = (props: ShowAlertProps) => setState({ ...props, show: true });
8+
const open = useCallback(
9+
(props: ShowAlertProps) => setState({ ...props, show: true }),
10+
[setState]
11+
);
912

10-
const close = () => setState({ show: false });
13+
const close = useCallback(() => setState({ show: false }), [setState]);
1114

1215
const alertContextValue = { ...state, open, close };
1316

0 commit comments

Comments
 (0)