Skip to content

Instantly share code, notes, and snippets.

@andrelandgraf
Last active July 6, 2021 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrelandgraf/2a403e7ba5e39924bd8675c92d54ac9d to your computer and use it in GitHub Desktop.
Save andrelandgraf/2a403e7ba5e39924bd8675c92d54ac9d to your computer and use it in GitHub Desktop.
Communication of status / errors codes between client and server via URLSearchParams
import { useCallback, useEffect } from 'react';
import { useSearchParams } from 'react-router-dom';
export default function useErrorNotifications() {
const { pushNotification } = useContext(NotificationsContext);
const [searchParams, setSearchParams] = useSearchParams();
const errorCode = searchParams.get('error');
const onClose = useCallback(() => {
searchParams.delete('error');
setSearchParams(searchParams, { replace: true });
}, [searchParams, setSearchParams]);
useEffect(() => {
if (errorCode) {
const [title, message] = getErrorMessage(errorCode);
pushNotification({
title: title,
message: message,
isPermanent: true,
isToast: false,
type: MessageType.error,
onClose,
});
}
}, [errorCode, onClose]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment