React 18 使用antd的message,notification组件报错createRoot warning

在使用react18.2和antd4.19时,由于ReactDOM.render方法已弃用,出现UsecreateRootinstead的错误。升级antd到4.24.12及更高版本,可以消除message和notification组件的警告,这些警告在React18中因使用ReactDOM.render而触发。

react版本:18.2

antd版本:4.19

报错:Use createRoot instead" occurs because the ReactDOM.render method has been deprecated. To solve the error, create a root element and use the ReactDOMClient.render method instead. This occurs since the render () method of the react-dom package is considered legacy starting react-dom version 18.

试了antd的4.17版本 4.19版本 4.19.5版本 问题都存在

解决:升级到文档上最新4.x版本:4.24.12

就可以了 不报错了

参考:

升级 React18 后 message, notification两个组件会提示警告 · Issue #34823 · ant-design/ant-design · GitHub

fix: Notification & message throw `createRoot` warning in React 18 by zombieJ · Pull Request #35030 · ant-design/ant-design · GitHub

fix: React 18 use createRoot instead by zombieJ · Pull Request #203 · react-component/notification · GitHub

React组件使用 Ant Design 的 `Notification` 组件,通常可以通过调用 `notification.open()` 方法来实现。该方法支持传入多个配置项,例如 `message`、`description`、`duration` 等,用于自定义通知的显示效果[^1]。 以下是一个完整的使用示例: ```jsx import React from 'react'; import { Button, notification } from 'antd'; class NotificationDemo extends React.Component { openNotification = () => { notification.open({ message: '这是一个通知', description: '这是 Ant Design 的 Notification 组件在类组件中的使用示例。', duration: 3, // 设置通知显示时长,单位为秒 placement: 'topRight', // 设置通知显示位置 }); }; render() { return ( <div> <Button type="primary" onClick={this.openNotification}> 显示通知 </Button> </div> ); } } export default NotificationDemo; ``` ### 配置说明 - `message`: 必填项,用于设置通知的标题。 - `description`: 可选,用于设置通知的详细描述内容。 - `duration`: 可选,设置通知自动关闭的延时,单位为秒,默认值为 4.5 秒。 - `placement`: 可选,设置通知显示的位置,支持 `topLeft`、`topRight`、`bottomLeft`、`bottomRight`。 - `onClick`: 可选,设置点击通知时的回调函数。 如果需要自定义通知图标,可以使用 `icon` 属性: ```jsx notification.open({ message: '自定义图标', description: '这是带有自定义图标的通知示例。', icon: <span role="img" aria-label="custom-icon">🔔</span>, }); ``` 此外,如果需要手动关闭通知,可以使用 `notification.close(key)` 方法,其中 `key` 是通知的唯一标识符。 ```jsx const key = 'custom_key'; notification.open({ key, message: '可手动关闭的通知', description: '您可以点击下方按钮手动关闭该通知。', }); // 手动关闭通知 notification.close(key); ``` ### 注意事项 - `notification.open()` 是一个静态方法,因此可以在类组件的方法中直接调用。 - 如果需要多次调用不同的通知,确保为每个通知分配唯一的 `key`,以避免冲突。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学途路漫漫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值