无法读取null的属性'setState' - React.js,Modal,Bootstrap

nob*_*are 3 reactjs react-bootstrap

我在尝试构建这个Web应用程序时第一次尝试React.js.我正在Cannot read property 'setState' of null下面的指定行.我一直试图找出过去几个小时的原因,似乎无法弄明白.任何帮助将不胜感激.

MyModal.jsx

import React from 'react';
import { Modal, Button, ButtonToolbar } from 'react-bootstrap';

export default class MyModal extends React.Component {
    constructor(props) {
        super(props);
        this.state = {show: false};
    }

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

    hideModal() {
        this.setState({show: false});
    }

    render() {
      return (
        <ButtonToolbar>
          <Button bsStyle="primary" onClick={this.showModal}>
            Launch demo modal
          </Button>

          <Modal
            {...this.props}
            show={this.state.show}
            onHide={this.hideModal}
            dialogClassName="custom-modal"
          >
            <Modal.Header closeButton>
              <Modal.Title id="contained-modal-title-lg">Modal heading</Modal.Title>
            </Modal.Header>
            <Modal.Body>
              <h4>Wrapped Text</h4>
              <p>Blah</p>
            </Modal.Body>
            <Modal.Footer>
              <Button onClick={this.hideModal}>Close</Button>
            </Modal.Footer>  // Chrome inspector says it errors on this line
          </Modal>
        </ButtonToolbar>
        );
    } // end render function

} // end export default
Run Code Online (Sandbox Code Playgroud)

Yad*_*ran 8

绑定你的组件类方法constructor()(绑定内部构造函数比内部绑定更好render()):

constructor(props) {
    super(props);
    this.state = {show: false};
    this.showModal = this.showModal.bind(this);
    this.hideModal = this.hideModal.bind(this);
}
Run Code Online (Sandbox Code Playgroud)