我使用的是React 18,MUI version 5,nanostores version 0.7.4。我写了一个名为useApi.tsx
的钩子,每当我使用这个钩子时,它都会为我初始化一个新的axios示例。现在在这个钩子中,我定义了一个拦截器来监听错误。它工作得很好,不管是什么错误,它都会调用responseErrorHandler
方法。代码如下:
import axios from 'axios';
import { Notification } from '../components/Notification/Notification.component';
export function useApi() {
const api = axios.create({
baseURL: import.meta.env.VITE_BACKEND_URL,
});
api.interceptors.response.use(null, responseErrorHandler);
return { api };
}
function responseErrorHandler(error) {
<Notification
color="error"
text="Something went wrong, please try again ..."
/>;
return Promise.reject(error);
}
下面是Notification
组件代码:
import {
Alert,
AlertColor,
Snackbar,
SnackbarCloseReason,
} from '@mui/material';
import { useStore } from '@nanostores/react';
import { useEffect } from 'react';
import { NotificationStateStore } from './Notification.store';
export function Notification({
text,
color,
autoHideDuration = 6000,
}: {
text: string;
color: AlertColor;
autoHideDuration?: number;
}) {
const { actions, states } = NotificationStateStore;
const notificationState = useStore(states.notificationState);
const handleClose = (
event: Event | React.SyntheticEvent<any, Event>,
reason?: SnackbarCloseReason,
) => {
if (reason === 'clickaway') {
return;
}
actions.closeNotification();
};
useEffect(() => {
actions.showNotification();
}, []);
return (
<Snackbar
open={notificationState}
autoHideDuration={autoHideDuration}
onClose={handleClose}
>
<Alert
onClose={handleClose}
severity={color}
sx={{ width: '100%' }}
>
{text}
</Alert>
</Snackbar>
);
}
正如你所看到的,我调用了actions.showNotification();
来向用户显示通知,下面是Notification.store.ts
内部的代码:
import { action, atom } from 'nanostores';
const notification = atom<boolean | undefined>();
const showNotification = action(
notification,
'showNotification',
(state) => {
state.set(true);
},
);
const closeNotification = action(
notification,
'closeNotification',
(state) => {
state.set(false);
},
);
export const NotificationStateStore = {
states: { notificationState: notification },
actions: { showNotification, closeNotification },
};
我的问题是它不起作用,我的意思是它不会在前端显示通知。这里你可以看到我在github中的代码:https://github.com/kasir-barati/open-ai-summaries/tree/d717f7e64d9e529f6d6c2aa698b464814512b29e
1条答案
按热度按时间63lcw9qa1#
你试图在一个普通的JavaScript函数(
responseErrorHandler
)中呈现一个React组件,甚至没有返回它。你可以在responseErrorHandler之外管理通知的状态,并在组件中使用它。NotificationStateStore
存储通知文本和颜色:responseErrorHandler
使用showNotification
操作:Notification
组件使用新的状态结构,并在应用程序根组件中呈现它:在您的应用程序或其他组件中,您要呈现通知: