javascript - 如何在延迟后显示模态?

标签 javascript reactjs

我试图在页面加载后 2 秒加载模式。我尝试在 componentDidUpdate 上设置状态,但我不断获得 active: undefined active 属性决定了页面上模态的可见性。我尝试在 react 工具的浏览器上将其切换为 true,然后我的模式出现了。我只是不确定如何在页面加载后 2 秒加载它。

  state = { show: true };

  showModal = () => {
    this.setState({ show: true });
  };

  closeModal = () => {
    this.setState({ show: false });
  };

render() {
const { showModal } = this.state;

return (
  <React.Fragment>
    ....
    <Modal.ModalAnimator active={showModal} onClose={this.closeModal}>
      <Modal.ModalWithCross
        onClose={this.closeModal}
      >
        <h3>Are you interested in any other Additions?</h3>
        <Section>
          <p>Hit “notify concierge” and we’ll be in touch shortly.</p>
        </Section>

      </Modal.ModalWithCross>
    </Modal.ModalAnimator>
  </React.Fragment>
)
}

最佳答案

在解构状态时,您可以编写 showModal 而不是实际的状态字段名称 show。因此,render 函数中的第一行应为:

render() {
  const { show } = this.state;

  return (
    <React.Fragment>
      ...
      <Modal.ModalAnimator active={show} onClose={this.closeModal}>
        ...

关于javascript - 如何在延迟后显示模态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58523917/

相关文章:

javascript - React 组件不是从 bootstrap 选项卡调用的吗?

javascript - 揭示模块模式 - 使用 Jasmine 进行单元测试

javascript - D3js 找到圆上最近的点

javascript - 使用 TypeScript 强制 React 组件命名

javascript - 如何在 C# 中的类库中运行 npm 命令

reactjs - react : Reactstrap pagination active not working inside loop

javascript - 查询 MongoDB 中的嵌套数组

javascript - TypeError : chunk. isInitial 不是函数

javascript - 如何在最小化屏幕时显示按钮?

reactjs - 将自定义类与 getFieldDecorator 结合使用(React、Ant Design)