From 11dfeb16f2b0a4824ff6804295259975adae3269 Mon Sep 17 00:00:00 2001 From: Louis Barranqueiro Date: Sat, 13 Mar 2021 10:54:21 -0800 Subject: [PATCH] fix: wrap functions provided by NotificationsProvider with useCallback (#374) - close #372 --- src/components/NotificationsProvider.tsx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/components/NotificationsProvider.tsx b/src/components/NotificationsProvider.tsx index 2e1c9cc1..2348a8cf 100644 --- a/src/components/NotificationsProvider.tsx +++ b/src/components/NotificationsProvider.tsx @@ -1,5 +1,5 @@ import React from 'react' -import {ReactNode, useReducer} from 'react' +import {ReactNode, useReducer, useCallback} from 'react' import notificationsReducer from '../reducers/notifications/reducer' import {NewNotification} from '../reducers/notifications/types' import {dismissNotification, dismissNotifications, notify} from '../reducers/notifications/actions' @@ -13,13 +13,16 @@ export const NotificationsProvider = (props: Props) => { const [notifications, dispatch] = useReducer(notificationsReducer(), []) const context = { notifications, - notify: (notification: NewNotification) => { - const action = notify(notification) - dispatch(action) - return action.payload - }, - dismissNotification: (id: string) => dispatch(dismissNotification(id)), - dismissNotifications: () => dispatch(dismissNotifications()), + notify: useCallback( + (notification: NewNotification) => { + const action = notify(notification) + dispatch(action) + return action.payload + }, + [dispatch] + ), + dismissNotification: useCallback((id: string) => dispatch(dismissNotification(id)), [dispatch]), + dismissNotifications: useCallback(() => dispatch(dismissNotifications()), [dispatch]), } return {props.children}