Why a newline between `return` and `(` breaks the code? [duplicate]

三世轮回 提交于 2019-12-25 12:33:45

问题


Based on the solution of my question: setState fires and render method gets hit, but nothing rerenders

Code works if there is NO newline between return and the (, and fails otherwise.

Example:

this works:

render() 
{
        return (
            <View >
                <ListView dataSource={this.state.beers_ds} renderRow={renderRow.bind(this)} />
            </View>
        ); 
} 

But this fails:

render() 
{
        return 
        (
            <View >
                <ListView dataSource={this.state.beers_ds} renderRow={renderRow.bind(this)} />
            </View>
        ); 
} 

Why?


回答1:


Seems like Automatic Semicolon Insertion might be biting you in the butt. I believe javascript will insert a ; at the end of a return statement automatically.

Why doesn't a Javascript return statement work when the return value is on a new line?




回答2:


As answered here: Javascript function fails to return object when there is a line-break between the return statement and the object?

it's just a matter of JS syntax. Semicolons are automatically added and thus the compiler treats

return
  ( sth )

as

return;
  ( sth )


来源:https://stackoverflow.com/questions/46309513/why-a-newline-between-return-and-breaks-the-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!