Hi,
I’m using the useToast hook and I often get the following error:
TypeError: Cannot read properties of undefined (reading 'unsubscribe')
So toast is undefined, as the error is in reference to the useEffect cleanup in the below code:
function useToast() {
var app = useAppBridge_1.useAppBridge();
var toast;
var show = react_1.useCallback(function (content, options) {
toast = Toast_1.create(app, {
message: content,
isError: options === null || options === void 0 ? void 0 : options.isError,
duration: (options === null || options === void 0 ? void 0 : options.duration) || exports.DEFAULT_TOAST_DURATION,
});
toast.dispatch(Toast_1.Action.SHOW);
if (options === null || options === void 0 ? void 0 : options.onDismiss) {
toast.subscribe(Toast_1.Action.CLEAR, options === null || options === void 0 ? void 0 : options.onDismiss);
}
}, [app]);
// HERE
react_1.useEffect(function () {
return function () {
toast.unsubscribe();
};
}, []);
return { show: show };
}
exports.useToast = useToast;
This will happen as I navigate between pages, or as I’m developing with hot reloading. The hot reloading part isn’t an issue, but the navigating is as this will happen while the app is live. Is there any reason that is in my control that toast would be undefined at any point? Or would the library need to be amended to check if it exists first? And if so, what’s the alternative in the meantime, use the Polaris component?
If you need any more information let me know, thanks!
Best,
Curtis