简体   繁体   中英

Unexpected token on render react

I have the following code which I expect would display a list of keys and values:

 render() { let content; if (this.props.json != null) { content = { Object.keys(this.props.json.fields).map(function (key) { return <div>Key: {key}, Value: {this.props.json.fields[key]}</div>; }) } } return ( <div> {content} </div> ) } 

However, for some reason i get this error:

Parsing error: Unexpected token, expected ","

Which points to the "Object.keys" I don't understand what I'm doing wrong, I've searched the internet and have seen ALOT of examples like this which work

Since you're wrapping "Object.keys..." with curly braces, you're declaring the content variable as an object. You can declare it without the curly braces, then content will be an array, and that should render properly.

if (this.props.json != null) {
    content = Object.keys(this.props.json.fields).map(function (key) {
        return <div>Key: {key}, Value: {this.props.json.fields[key]}</div>;
    });
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM