javascript - React bootstrap 重复模态组件

标签 javascript reactjs bootstrap-modal react-bootstrap

enter image description here

由于某种原因,模态框被渲染了不止一次,有时是 2 或 3 次。然后,几秒钟后,附加模态框会自动移除。

模式是通过一条路线打开的,所以我正在做这样的事情:

const ModalWrapper = (props) => {
  return (
    <Modal
      show={props.showModal}
      onHide={props.hide}
    >
    ...
    </Modal>
  );
};

class ComponentPage extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      showModal: true,
    };
  }

  @autobind
  closeModal() {
    this.props.history.goBack();
    this.setState({showModal: false});
  }

  render() {
    return (
      <ModalWrapper
        {...this.state}
        hide={this.closeModal}
      />
    );
  }
}

最佳答案

通常这发生在模态框位于包装器内时,例如:

const ChildrenWrapper = ({children}) => {
  return <div>{children}</div>
}

模态框是包装器中的子元素:

const ModalWrapped= ({}) => {
return <ChildrenWrapper>
            <Modal show={true}>some content</Modal>
       </ChildrenWrapper>
}

比应用程序:

const App = () => {
   return <ModalWrapped/>
}

结果是 Modal 的实例在虚拟 dom 中渲染了 2 次。

关于javascript - React bootstrap 重复模态组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761650/

相关文章:

javascript - 将索引动态附加到属性

javascript - 数据表 - 使用 fnCreatedRow 添加详细信息行

javascript - 对象中 float 的总和。 ( typescript )

javascript - 单击按钮时滑出链接

JavaScript 根据索引设置 URL

javascript - 从另一个类 React Native 访问它

javascript - 当外部值发生变化时如何使组件重新渲染?

reactjs - Selenium-ide,文件上传不适用于 react 应用程序

javascript - 如何在 Bootstrap 模式将它添加到正文以调整窗口滚动时获取 padding-right 变量的值?

angularjs - 模态内的 NgMap(谷歌地图) - 不显示第二次