【问题标题】:Removing an external component on click of button单击按钮删除外部组件
【发布时间】:2021-12-25 09:54:50
【问题描述】:

我是 React 新手。我在 index.js React DOM 区域中有以下组件

ReactDOM.render(
  <React.StrictMode>
    <NavigationBar />
    <App />
    <Login />
    
  </React.StrictMode>,
  document.getElementById('root')
);

NavigationBar 是使用 Bootstrap 创建的,当我单击搜索按钮时,我希望将组件删除并替换为组件。

我该怎么做?

【问题讨论】:

    标签: javascript reactjs components


    【解决方案1】:

    你需要使用 useState : 当您单击按钮时,您需要使用 onChange 更改状态

         const [check, setCheck] = useState(false);
        
            ReactDOM.render(
          <React.StrictMode>
            <NavigationBar />
            {check ? <App /> : anotherCoponent you want}
            <Login />
        
      </React.StrictMode>,
      document.getElementById('root')
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      相关资源
      最近更新 更多