Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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
Javascript 未捕获的TypeError:无法读取未定义的属性“state”_Javascript_Html_Css_Reactjs_Redux - Fatal编程技术网

Javascript 未捕获的TypeError:无法读取未定义的属性“state”

Javascript 未捕获的TypeError:无法读取未定义的属性“state”,javascript,html,css,reactjs,redux,Javascript,Html,Css,Reactjs,Redux,我正试图从React.createClass更改为React.Component,但我遇到了以下错误 Uncaught TypeError: Cannot read property 'state' of undefined 我在谷歌上搜索错误,但没有找到答案 类Accordion扩展了React.Component{ 你需要把这个绑起来 this.onSelect->this.onSelect.bindthis this.enhanceSection->this.enhanceSectio

我正试图从React.createClass更改为React.Component,但我遇到了以下错误

Uncaught TypeError: Cannot read property 'state' of undefined 
我在谷歌上搜索错误,但没有找到答案

类Accordion扩展了React.Component{ 你需要把这个绑起来

this.onSelect->this.onSelect.bindthis

this.enhanceSection->this.enhanceSection.bindthis

您需要将onSelect绑定到构造函数中的类:

constructor(props) {
    super(props);
    this.state = {
        selected: props.selected // use props, not this.props
    };
    this.onSelect = this.onSelect.bind(this);
}
extends语法不再像createClass那样具有自动绑定功能

您需要绑定此。onSelect 如果忘记绑定this.onSelect并将其传递给onClick,则在实际调用该函数时,将无法定义该函数

试试这个:

类SectionAccordion扩展了React.Component{ 构造器{ 超级作物; this.onSelect=this.onSelect.bindthis; } 当选{ this.props.\u选择this.props.id; } 渲染{ console.log手风琴/手风琴部分组件; var className='accordion section'+此.props._选定?'selected':; 回来 {this.props.title} {this.props.children} ; }
}嘿,那个被修复了,但我在这里得到了孩子未定义的错误id=child.props.id;@im你能告诉我为什么它有助于理解嘿,那个被修复了,但我在这里得到了孩子未定义的错误id=child.props.id;你能告诉我为什么它有助于理解嘿,那个被修复了,但我得到了孩子未定义的错误他re id=child.props.id;您能告诉我它有助于理解JavaScript的原因吗?在默认情况下,类方法不绑定。如果您忘记绑定this.onSelect并将其传递给onClick,则在实际调用函数时,这将是未定义的。这不是特定于React的行为,而是JavaScript.Generall中函数工作方式的一部分y、 如果引用一个不在其后的方法,例如onClick={this.onSelect},则应该绑定该方法。。