Javascript Can';在Object.keys中找不到状态

Javascript Can';在Object.keys中找不到状态,javascript,reactjs,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 con

我做错了什么

谢谢

试试这个:

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() {...}
}
或者更好,如果您有
ES6

Object.keys(configOptions).map(function(key) {
    console.log('test'); 
    console.log(this.state.myStateValue); 
}.bind(this))
试试这个:

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() {...}
}
或者更好,如果您有
ES6

Object.keys(configOptions).map(function(key) {
    console.log('test'); 
    console.log(this.state.myStateValue); 
}.bind(this))

我只是复制了OP的片段。大概他只是想测试
this.state
的可见性,稍后会在循环中做更多的工作。@user1628461成功了!!非常感谢。我想我需要更多地了解this@user1628461哦,是的,你是对的,我错了。我只是复制了OP的片段。大概他只是想测试
this.state
的可见性,稍后会在循环中做更多的工作。@user1628461成功了!!非常感谢。我想我需要更多地了解this@user1628461哦,是的,你是对的,我错了。