Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 无法读取属性';设置状态';null-React.js、Modal、Bootstrap的_Reactjs_React Bootstrap - Fatal编程技术网

Reactjs 无法读取属性';设置状态';null-React.js、Modal、Bootstrap的

Reactjs 无法读取属性';设置状态';null-React.js、Modal、Bootstrap的,reactjs,react-bootstrap,Reactjs,React Bootstrap,我第一次尝试React.js来构建这个web应用程序。我在下面指定的行中获得了null的无法读取属性'setState'。在过去的几个小时里,我一直在试图找出原因,但似乎无法找到答案。任何帮助都将不胜感激 MyModal.jsx import React from 'react'; import { Modal, Button, ButtonToolbar } from 'react-bootstrap'; export default class MyModal extends React.

我第一次尝试React.js来构建这个web应用程序。我在下面指定的行中获得了null的
无法读取属性'setState'。在过去的几个小时里,我一直在试图找出原因,但似乎无法找到答案。任何帮助都将不胜感激

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
从“React”导入React;
从“react bootstrap”导入{Modal,Button,buttonoolbar};
导出默认类MyModal扩展React.Component{
建造师(道具){
超级(道具);
this.state={show:false};
}
showModal(){
this.setState({show:true});
}
hideModal(){
this.setState({show:false});
}
render(){
返回(
启动演示模式
模态标题
包装文本
废话

接近 //Chrome inspector说这行有错误 ); }//结束渲染函数 }//结束导出默认值
构造函数()中绑定组件类方法。
(为了性能起见,在构造函数中绑定要比在
呈现()中绑定好):


哦,太好了!我还发布了一个解决方案,但我更喜欢你的,所以我将删除我的。我们不希望
.bind
在每个渲染上创建一个新函数:)这完全解决了我的问题,但一个合理的问题是,为什么这个样板文件首先是必要的,为什么没有办法用更少的手工编写的代码来自动化它,我很想为我做这件事,这样我就不能把它搞砸而忽略它。真令人失望。可能的答案是:这是因为构造函数()位于组件的“静态”类中,而不是实例方法。蹩脚的“惩罚程序员”API design.ty Jared。React有一些严重的设计缺陷,更不用说极端的复杂性和学习曲线了。我的朋友发现angular2对Rock来说这实际上不是一个特殊的问题。这就是JavaScript的工作原理。如果您在es5中编写类(使用
React.createClass
),类方法将自动绑定到该类。与其他框架相比,React并没有带来太多的学习曲线,因为它只代表MVC应用程序中的视图。
constructor(props) {
    super(props);
    this.state = {show: false};
    this.showModal = this.showModal.bind(this);
    this.hideModal = this.hideModal.bind(this);
}