使用@mantine/notifications v5.10.4时showNotification不存在问题求助
问题原因及解决方案
版本方法名差异:你使用的是Mantine v5版本(^5.10.4),该版本中
useNotifications返回的方法名为show,而showNotification是Mantine v6才新增的方法名,这是导致方法不存在的直接原因。修复方案:
- 将代码里的
notifications.showNotification替换为notifications.show - 确认应用根组件已包裹
NotificationsProvider——这是使用通知组件的必要前提,缺少它useNotifications无法正常初始化
- 将代码里的
修改后的代码示例:
import React from 'react'; import { Group, Button } from '@mantine/core'; import { useNotifications, NotificationsProvider } from '@mantine/notifications'; function Demo() { const notifications = useNotifications(); return ( <Group position="center"> <Button variant="outline" onClick={() => notifications.show({ title: 'Default notification', message: 'Hey there, your code is awesome! 🤥', }) } > Show notification </Button> </Group> ); } // 根组件必须包裹NotificationsProvider function App() { return ( <NotificationsProvider> <Demo /> </NotificationsProvider> ); }
内容的提问来源于stack exchange,提问作者whowhenhow
相关产品推荐
相关产品推荐

