Skip to content

Commit

Permalink
fix: add useCallback hook to toast method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Verster committed Jun 4, 2021
1 parent 110b9be commit ff0c604
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/Context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ const ToastProvider: React.FC<Omit<ToastContextType, 'toast'>> = ({
}) => {
const [toasts, setToasts] = React.useState<FullToastConfig[]>([])

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)
Expand Down

0 comments on commit ff0c604

Please # to comment.