最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make reactstrap modal open or close from parent component - Stack Overflow

programmeradmin1浏览0评论

I am trying to open / close a reactstrap modal from a parent ponent button, but I cannot get it to work.

I am passing the state isModalOpen as a prop to the child ModalExample ponent, and it changes as shown in the debug text fields I made, but the modal does not seem to open.

Any help is appreciated. Here is a jsfiddle: /

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 modo 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"));

I am trying to open / close a reactstrap modal from a parent ponent button, but I cannot get it to work.

I am passing the state isModalOpen as a prop to the child ModalExample ponent, and it changes as shown in the debug text fields I made, but the modal does not seem to open.

Any help is appreciated. Here is a jsfiddle: https://jsfiddle/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 modo 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"));
Share Improve this question edited Aug 6, 2018 at 23:27 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Aug 6, 2018 at 23:26 HilyinHilyin 1131 gold badge1 silver badge6 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

You are passing the state variable isModalOpen both as the variable indicating if it's open, and the function to use to toggle it. Use this.toggle for toggling instead.

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

You are also using the toggleModalView prop in the ModalExample ponent, but you are passing in the function as toggle, so you should use this.props.toggle instead. You are also passing in isModalOpen as isOpen, so you should use this.props.isOpen instead.

    import React from 'react';
import { Button, Modal, ModalFooter, ModalHeader,ModalBody} from 'reactstrap';


class ModalExample extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            modal: props.initialModalState,
            fade: true
        };

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

    toggle() {
        this.setState({
            modal: !this.state.modal,
            fade: !this.state.fade

        });
    }

    render() {
        return (
            <div>
                <Button color="danger" onClick={this.toggle}>TOGGLE</Button>
                <Modal isOpen={this.state.modal} toggle={this.toggle}
                       fade={this.state.fade}
                       className={this.props.className}>
                    <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
                    <ModalBody>



                    </ModalBody>
                    <ModalFooter>
                        <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
                        <Button color="secondary" onClick={this.toggle}>Cancel</Button>
                    </ModalFooter>
                </Modal>
            </div>
        );
    }
}

export default class SampleApp extends React.Component {
    constructor(props) {
        super(props);
        this.state = {}
    }

    render() {
        return (
            <div>
                <ModalExample initialModalState={false} />
            </div>
        )
    }
}

**This example getting call ponent modal from another ponent**
发布评论

评论列表(0)

  1. 暂无评论