Showing Notifications on state/context change #58
-
Hello, Fantastic library, thank you! Could anyone explain to me how to call show a notification based on a change of state or context, please? Something like:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi Matt, the best way to do this is to integrate const [loading, setLoading] = useState(false);
const notifications = useNotifications();
const startLoading = () => {
setLoading(true);
notifications.showNotification({ loading: true, message: 'Loading your data' });
} You can also subscribe to state via useEffect at any component (not recommending this though as it will be hard to debug this in future): useEffect(() => {
loading &&
notifications.showNotification({
loading: true,
message: "Loading your data",
});
}, [loading]); |
Beta Was this translation helpful? Give feedback.
-
I just threw together this example. https://codesandbox.io/s/notification-state-change-358cc It is watching a state variable, but could just as easy be watching any input prop too. |
Beta Was this translation helpful? Give feedback.
Hi Matt, the best way to do this is to integrate
notifications.showNotification
handler directly into where state is managed, for example in function where you fetch the data:You can also subscribe to state via useEffect at any component (not recommending this though as it will be hard to debug this in future):