Javascript “反应”;TypeError:无法读取属性';州';“未定义”的定义;“当在里面时”;“地图”;
正如你在下面看到的。第一张照片在Javascript “反应”;TypeError:无法读取属性';州';“未定义”的定义;“当在里面时”;“地图”;,javascript,reactjs,state,material-ui,Javascript,Reactjs,State,Material Ui,正如你在下面看到的。第一张照片在 this.state.blabal 不在房间里 map(a, b){blabla} map(a, b){`here!!`} {Object.keys(newsProviderID_Name_Dic).map(function(key, index){ return<FormControlLabel control={
this.state.blabal
不在房间里
map(a, b){blabla}
map(a, b){`here!!`}
{Object.keys(newsProviderID_Name_Dic).map(function(key, index){
return<FormControlLabel
control={
<Switch
checked={this.state.gilad}
onChange={this.handleChange('gilad')}
value="gilad"
/>
}
label="Gilad Gray"
/>;
})}
但就像照片2一样,当我将工作的精细块移动到
map(a, b){blabla}
map(a, b){`here!!`}
{Object.keys(newsProviderID_Name_Dic).map(function(key, index){
return<FormControlLabel
control={
<Switch
checked={this.state.gilad}
onChange={this.handleChange('gilad')}
value="gilad"
/>
}
label="Gilad Gray"
/>;
})}
map(a,b){`here!!`}
{Object.keys(newsProviderID\u Name\u Dic).map(函数(键,索引){
回来
})}
它将显示错误:
TypeError:无法读取未定义的属性“状态”
为什么会这样?如何在没有单独约束的情况下解决这个问题
使用箭头功能
Object.keys(newsProviderID_Name_Dic).map((key, index) => {
return () } )
在箭头函数中,此
保持与封闭范围相同的含义使用箭头函数
Object.keys(newsProviderID_Name_Dic).map((key, index) => {
return () } )
在箭头函数中,
此
保持与封闭范围相同的含义可能是您未设置组件的初始状态。
你试试看:
class Example extends Component {
state:{}
render(){
//.....code
}
}
或
}
祝你好运 可能是您没有为组件设置初始状态。 你试试看:
class Example extends Component {
state:{}
render(){
//.....code
}
}
或
}
祝你好运 虽然总是有一个初始状态是个好主意,但这并不是问题的原因。您可以从错误消息中看到,
此
已经是未定义的
,因为map()
函数的回调未绑定到组件实例。虽然始终具有初始状态是一个好主意,但这并不是问题的原因。您可以从错误消息中看到,由于map()
函数的回调未绑定到组件实例,因此此
已未定义
。