-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8136 from marmelab/fix-usenotify-triggers-rerenders
Fix useNotify triggers useless rerenders
- Loading branch information
Showing
6 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createContext } from 'react'; | ||
|
||
import { NotificationPayload } from './types'; | ||
|
||
export const AddNotificationContext = createContext< | ||
(notification: NotificationPayload) => void | ||
>(() => {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from './types'; | ||
export * from './useNotify'; | ||
export * from './AddNotificationContext'; | ||
export * from './NotificationContext'; | ||
export * from './NotificationContextProvider'; | ||
export * from './types'; | ||
export * from './useAddNotificationContext'; | ||
export * from './useNotificationContext'; | ||
export * from './useNotify'; |
5 changes: 5 additions & 0 deletions
5
packages/ra-core/src/notification/useAddNotificationContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { useContext } from 'react'; | ||
import { AddNotificationContext } from './AddNotificationContext'; | ||
|
||
export const useAddNotificationContext = () => | ||
useContext(AddNotificationContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react'; | ||
import { useNotify } from './useNotify'; | ||
import { useNotificationContext } from './useNotificationContext'; | ||
import { NotificationContextProvider } from './NotificationContextProvider'; | ||
|
||
export default { | ||
title: 'ra-core/useNotify', | ||
}; | ||
|
||
const Button = () => { | ||
const notify = useNotify(); | ||
const handleClick = React.useCallback(() => { | ||
notify('hello'); | ||
}, [notify]); | ||
return <button onClick={handleClick}>Notify</button>; | ||
}; | ||
|
||
const Notifications = () => { | ||
const { notifications } = useNotificationContext(); | ||
return ( | ||
<ul> | ||
{notifications.map(({ message }, id) => ( | ||
<li key={id}>{message}</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
export const Basic = () => ( | ||
<NotificationContextProvider> | ||
<Button /> | ||
<Notifications /> | ||
</NotificationContextProvider> | ||
); | ||
|
||
export const ManyListeners = () => { | ||
const times = new Array(100).fill(0); | ||
return ( | ||
<NotificationContextProvider> | ||
{times.map((_, index) => ( | ||
<Button key={index} /> | ||
))} | ||
<div> | ||
<Notifications /> | ||
</div> | ||
</NotificationContextProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters