【问题标题】:Creating Modal Component with Separate Modal Trigger Button使用单独的模态触发按钮创建模态组件
【发布时间】:2017-10-11 23:54:38
【问题描述】:

我是 React 新手,请耐心等待。

我正在尝试创建一个模态组件,该组件将从任何全局元素的 onClick() 函数触发,例如:链接、按钮、跨度或整个应用程序中的任何内容。

import React from 'react';
import ReactDOM from 'react-dom';


const display = {
  display: 'block'
};

const hide = {
  display: 'none'
};


class Modal extends React.Component {

    constructor(props) {
      super(props);
      this.toggle = this.toggle.bind(this);
      this.state = {
        toggle: false
      }
    }

    toggle(event) {
      this.setState(prevState => ({
        toggle: !prevState.toggle
      }));
    }

    render() {
        return (
          <div>
            <button onClick={this.toggle}>Show Modal</button>
            <div className="modal" style={this.state.toggle ? display : hide} >
              <div className="modal-content">
                {this.props.children}
                <button onClick={this.toggle}>Close</button>
              </div>
            </div>
          </div>
        );
      }

}


module.exports = Modal;

【问题讨论】:

  • 如果没有像 Redux 或 MobX 这样的状态管理库,这将非常乏味。看看那些。
  • 使用react-bootstrap-modal怎么样?如果可以的话,我可以给你举个例子
  • 当然,我愿意接受更多想法
  • 刚刚添加了一个答案,请尝试一下并告诉我您的控制台是否有任何错误,谢谢

标签: javascript reactjs babeljs jsx


【解决方案1】:

你可以使用react-bootstrap-modal:

npm install --save react-bootstrap-modal

然后在你的组件中:

import React from 'react';
import Modal from 'react-bootstrap-modal';

export default class ModalExample extends React.Component {

  constructor(props){
    super(props);
    this.state = {
      open: false,
    }
  }

  openModal = () => this.setState({ open: true })

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

  render(){

    return (
      <div>
        <button type='button' onClick={this.openModal}>Launch modal</button>

        <Modal
          show={this.state.open}
          onHide={this.closeModal}
          aria-labelledby="ModalHeader"
        >
          <Modal.Header>
            <Modal.Title id='ModalHeader'>A Title Goes here</Modal.Title>
            <div onClick={this.closeModal}>CLOSE HERE</div>
          </Modal.Header>
          <Modal.Body>
            <p>Some Content here</p>
          </Modal.Body>
          <Modal.Footer>
            // If you don't have anything fancy to do you can use
            // the convenient `Dismiss` component, it will
            // trigger `onHide` when clicked
            <Modal.Dismiss className='btn btn-default'>Cancel</Modal.Dismiss>

            // Or you can create your own dismiss buttons
            <button className='btn btn-primary' onClick={this.closeModal}>
              CLOSE HERE TOO
            </button>
          </Modal.Footer>
        </Modal>
      </div>
    )
  }
}

更多参考,请到这里:

https://github.com/jquense/react-bootstrap-modal

如有必要,您可能还需要包含 bootstrap-CSS 文件!如有错误,请在此处发布,谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多