Javascript 此选项在setState上未定义
我知道这是一个有约束力的问题,但我不知道该在哪里约束它。我试过几种不同的方法 问题发生在应用程序上。\u当用户更改下拉框时选择。我希望它更新状态,以强制日历组件重新加载 应用程序Javascript 此选项在setState上未定义,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我知道这是一个有约束力的问题,但我不知道该在哪里约束它。我试过几种不同的方法 问题发生在应用程序上。\u当用户更改下拉框时选择。我希望它更新状态,以强制日历组件重新加载 应用程序 class App extends React.Component { constructor() { super(); this.state = { room: "A" } } _select(e) {
class App extends React.Component {
constructor() {
super();
this.state = {
room: "A"
}
}
_select(e) {
this.setState({
room: e.target.value
});
}
render() {
return (
<div>
<Selector select={this._select}/>
<Calendar room={this.state.room}/>
</div>
);
}
};
export default App;
类应用程序扩展了React.Component{
构造函数(){
超级();
此.state={
房间:“A”
}
}
_选择(e){
这是我的国家({
房间:e.target.value
});
}
render(){
返回(
);
}
};
导出默认应用程序;
选择器
const Selector = (props) => {
var items = API.rooms.map((item) => {
return(
<option value={item.room}>Study Room {item.room}</option>
);
});
return (
<div className="mat-section mat-section--m mat--color-primary">
<div class="mat-section__body">
<div class="custom-select">
<select onChange={props.select}>
{items}
</select>
<div class="custom-select__arrow"></div>
</div>
</div>
</div>
);
}
export default Selector;
const选择器=(道具)=>{
变量项=API.rooms.map((项)=>{
返回(
自修室{item.Room}
);
});
返回(
{items}
);
}
导出默认选择器;
在构造函数中
this._select = this._select.bind(this);
或es6定义的函数
const _select = () => {....}
您需要将
此
绑定到\u select()
,这样它就在您的方法中定义了
constructor() {
super();
this.state = {
room: "A"
};
this._select = this._select.bind(this);
}
如果使用箭头函数,绑定将自动完成,这也是一个选项
干杯。为什么要投反对票?