无法在Object.keys中找到状态

时间:2017-05-31 18:06:22

标签: javascript reactjs

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() {...}
}

我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

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); 
})