且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在React中单击按钮时显示组件

更新时间:2023-01-16 14:37:19

我已经设置了沙箱来展示如何做到这一点.

I've set up a sandbox to showcase how you can do this.

  1. 布尔值设置为false
  2. 的初始化状态
  3. 基于此布尔值有条件地渲染组件;因此最初该组件现在将显示在DOM上
  4. 在执行某些操作(onClick)时,将setState放在布尔值上以true
  5. 状态更改后,组件将重新呈现,现在将显示隐藏的组件(因为布尔值已设置为true)
  1. Initialise state with a boolean set to false
  2. Render the component conditionally based on this boolean; so initially the component will now show up on the DOM
  3. On some action (onClick), setState on the boolean to true
  4. The component will re-render since the state changed and will now show the hidden component (since the boolean has been set to true)