根据我按下左箭头键还是右箭头键的情况,我需要能够添加“活动”。本质上,我只是在浏览菜单。尝试设置状态时,我总是收到不确定的返回信息。
错误:“无法读取未定义的属性”光标”
Menu.js
...
class Menu extends Component {
constructor(props) {
super(props);
//set active-state to name of button label
this.state = {
activeTab: this.props.children[0].props.label,
cursor: 0,
};
}
...
答案 0 :(得分:0)
您需要像这样将handleKeyDown()
绑定到该类:
constructor(props) {
super(props);
//set active-state to name of button label
this.state = {
activeTab: this.props.children[0].props.label,
cursor: 0,
};
// bind the 'this' of the class to the function so this.state can be reached
this.handleKeyDown = this.handleKeyDown.bind(this);
}
基本上,每当需要使用需要使用this.state
之类的类变量的类方法时,都需要将“ this”绑定到函数。请参阅React Docs How can you dynamically create variables via a while loop? [duplicate]