Object.keys中找不到状态
问题描述:
render() {
console.log(this.state.myStateValue); // I see this on the console
var test = configOptions ?
Object.keys(configOptions).map(function(key) {
console.log('test'); // I see this on the console
console.log(this.state.myStateValue); // Getting Uncaught TypeError: Cannot read property 'state' of undefined
}
return() {...}
}
我在做什么错?Object.keys中找不到状态
谢谢!
答
试试这个:
Object.keys(configOptions).map(function(key) {
console.log('test');
console.log(this.state.myStateValue);
}.bind(this))
或更好,如果你有ES6
:
Object.keys(configOptions).map((key) => {
console.log('test');
console.log(this.state.myStateValue);
})
的可能的复制[?如何进入回调中正确的\'这\'上下文(HTTPS ://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) – noahnu