热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

记录学习React遇到的问题

1.React18解决unmountComponentAtNode()被弃用warning问题Warning:YouarecallingReactDOM.unmountCompon

1.React18 解决 unmountComponentAtNode()被弃用warning问题

Warning: You are calling ReactDOM.unmountComponentAtNode() on a container that was previously passed to ReactDOMClient.createRoot(). This is not supported. Did you mean to call root.unmount()?
Warning: unmountComponentAtNode(): The node you're attempting to unmount was rendered by React and is not a top-level container. Instead, have the parent component update its state and rerender in order to remove this component.

import React from 'react'
import { createRoot } from 'react-dom/client'
export default function Demo() {
const [count, setCount] = React.useState(0)
const [name, setName] = React.useState('tom')
function add() {
setCount(count + 1)
}
function changeName() {
setName('jack')
}
React.useEffect(() => {
const timer = setInterval(() => {
setCount(count => count + 1)
// setCount(count + 1)为什么无法正确出现效果
}, 1000)
return () => {
clearInterval(timer)
}
}, [])
function unMount() {
// ReactDOM.unmountComponentAtNode(document.getElementById('root'))
const root = createRoot(document.getElementById('root'));
root.unmount()
}
return (


当前求和为{count}
当前名字为{name}




)
}


推荐阅读
author-avatar
多米音乐_34473808
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有