From ff0c60488d20c24507a01b947e689be70ac99eca Mon Sep 17 00:00:00 2001 From: Jean Verster Date: Fri, 4 Jun 2021 13:45:57 +0200 Subject: [PATCH] fix: add useCallback hook to toast method --- src/Context/index.tsx | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Context/index.tsx b/src/Context/index.tsx index 6ced9f4..d79021a 100644 --- a/src/Context/index.tsx +++ b/src/Context/index.tsx @@ -48,21 +48,24 @@ const ToastProvider: React.FC> = ({ }) => { const [toasts, setToasts] = React.useState([]) - const toast = (newToast: ToastConfig) => { - LayoutAnimation.configureNext(CustomLayoutConfig) - setToasts((prevToasts) => { - const toasts = - position === 'BOTTOM' - ? [...prevToasts, { index: prevToasts.length, id: uuid(), ...newToast }] - : [{ index: prevToasts.length, id: uuid(), ...newToast }, ...prevToasts] - if (maxToasts && prevToasts.length === maxToasts) { - position === 'BOTTOM' ? toasts.shift() : toasts.pop() - return toasts - } else { - return toasts - } - }) - } + const toast = React.useCallback( + (newToast: ToastConfig) => { + LayoutAnimation.configureNext(CustomLayoutConfig) + setToasts((prevToasts) => { + const toasts = + position === 'BOTTOM' + ? [...prevToasts, { index: prevToasts.length, id: uuid(), ...newToast }] + : [{ index: prevToasts.length, id: uuid(), ...newToast }, ...prevToasts] + if (maxToasts && prevToasts.length === maxToasts) { + position === 'BOTTOM' ? toasts.shift() : toasts.pop() + return toasts + } else { + return toasts + } + }) + }, + [maxToasts] + ) const hideToast = (id: string) => { LayoutAnimation.configureNext(CustomLayoutConfig)