单击按钮,我如何从DOM中卸载功能组件
我想从DOM“卸载简单的功能组件”。我搜索了很多,并且看到大多数教程都是基于类组件的,但我没有看到任何简单的示例。我的要求是在单击按钮时从DOM中卸载功能组件。以下是我喜欢在单击时喜欢卸载的按钮的组件。希望有人可以帮助我做到这一点。提前致谢 !
import React from 'react'
function App() {
return (
<div className='app-component'>
<h2 className="h2">App Component</h2>
<button>Unmount This Component</button>
</div>
)
}
export default App
I would like to "Unmount a simple Functional Component" from the DOM. I searched a lot and saw most of the tutorials are based on Class Components and I did'nt see any simple example on it. My requirement is Unmounting a Functional component from the DOM on click on a button. Following is the component with the button which i likes to unmount when click on it. Hopes someone can help me to do it. Thanks in Advance !
import React from 'react'
function App() {
return (
<div className='app-component'>
<h2 className="h2">App Component</h2>
<button>Unmount This Component</button>
</div>
)
}
export default App
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要卸载组件,则可以使用有条件渲染,可以在其中声明父组件中的状态,并且基于状态可以安装或卸载组件,为:
这是您想要
oct 或
Unmount
code> codesandbox demo
>
parent component
儿童组件
If you want to unmount a component then you can use conditional rendering where you can declare state in parent component and based on the state you can mount or unmount component as:
This is the parent component from where you want to
mount
orunmount
CODESANDBOX DEMO
Parent component
Child component
您可以使用状态标志来删除这样的元素:
You can use state flag for removing element like this: