Skip to content

Commit

Permalink
fix: wrap functions provided by NotificationsProvider with useCallback (
Browse files Browse the repository at this point in the history
#374)

- close #372
  • Loading branch information
LouisBarranqueiro committed Mar 13, 2021
1 parent 6339b0b commit 11dfeb1
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/components/NotificationsProvider.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 <ReapopNotificationsContext.Provider value={context}>{props.children}</ReapopNotificationsContext.Provider>
Expand Down

0 comments on commit 11dfeb1

Please sign in to comment.