javascript - 如何从父组件打开或关闭 reactstrap 模态

标签 javascript reactjs reactstrap

我正在尝试从父组件按钮打开/关闭 react 器模式,但我无法让它工作。

我正在将状态 isModalOpen 作为 Prop 传递给子 ModalExample 组件,它的变化如我制作的调试文本字段所示,但模态没有似乎打开了。

感谢任何帮助。这是一个 jsfiddle:https://jsfiddle.net/67wy5nqp/

const { Button, Modal, ModalHeader, ModalBody, ModalFooter } = Reactstrap;

class ModalExample extends React.Component {
  constructor(props) {
    super(props);
    console.log(this.props);
  }

  render() {
    return (
      <div>
        <br />
        <label>
          (child) this.props.isOpen
          <input type="text" value={this.props.isOpen} />
        </label>

        <Modal
          isOpen={this.props.isModalOpen}
          toggle={this.props.toggleModalView}
          className={this.props.className}
        >
          <ModalHeader toggle={this.props.toggleModalView}>
            Modal title
          </ModalHeader>
          <ModalBody>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
            eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
            ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
            aliquip ex ea commodo consequat. Duis aute irure dolor in
            reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
            pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
            culpa qui officia deserunt mollit anim id est laborum.
          </ModalBody>
          <ModalFooter>
            <Button color="primary" onClick={this.props.toggleModalView}>
              Do Something
            </Button>
            <Button color="secondary" onClick={this.props.toggleModalView}>
              Cancel
            </Button>
          </ModalFooter>
        </Modal>
      </div>
    );
  }
}

class SampleApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isModalOpen: true
    };

    this.showModal = this.showModal.bind(this);
    this.toggle = this.toggle.bind(this);
  }

  toggle() {
    this.setState({
      isModalOpen: !this.state.isModalOpen
    });
  }

  showModal() {
    this.setState({
      isModalOpen: true
    });
  }

  render() {
    return (
      <div>
        <ModalExample
          isOpen={this.state.isModalOpen}
          toggle={this.state.isModalOpen}
        />

        <br />
        <button className="btn btn-primary" onClick={this.toggle}>
          Show Modal From Parent Component
        </button>

        <br />
        <label>
          (parent) this.state.isModalOpen:
          <input type="text" value={this.state.isModalOpen} />
        </label>
      </div>
    );
  }
}

ReactDOM.render(<SampleApp />, document.querySelector("#app"));

最佳答案

您正在传递状态变量 isModalOpen 作为指示它是否打开的变量,以及用于切换它的函数。请改用 this.toggle 进行切换。

<ModalExample
  isOpen={this.state.isModalOpen}
  toggle={this.toggle}
/>

您还在 ModalExample 组件中使用了 toggleModalView 属性,但是您将函数作为 toggle 传递,因此您应该使用this.props.toggle 代替。您还将 isModalOpen 作为 isOpen 传递,因此您应该改用 this.props.isOpen

关于javascript - 如何从父组件打开或关闭 reactstrap 模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716863/

相关文章:

javascript - 为什么 angularjs 中的 $asyncValidators 返回一个 promise 但未定义

javascript - 在按钮单击时添加动态下拉菜单并获取下拉值

javascript - jQuery 子菜单不会消失,除非它悬停一次

javascript - 如何通过Enzyme获取依赖组件元素?

bootstrap-4 - 页面右下角的 Bootstrap Toast

reactjs - 更改 Reactstrap 导航栏颜色和属性

javascript - PHP 页面上方法的进度

javascript - React 在小于号之前给出错误

reactjs - 视频源未在 useState() 上更新

reactjs - img 未显示在 reactstrap 轮播中