react native jsx中return需要注意的地方

看下面一个例子,定义一个MyModal组件,渲染hello world字样。

export default class MyModal extends Component {

    constructor(props) {
        super(props);

    };

    render() {
        return(
            <View>hello world</View>
        )
    }
}

一、以下方式则不会渲染任何结果,会报错。如果不是render下的根return不会报错,只是渲染内容为空

1、将“(”大括号换行

return
(
<View>hello world</View>
)

2、取消大括号 View标签换行

return
<View>hello world</View>


二、改成以下方式也是可行的

1、取消大括号,View标签和return同一行

return <View>hello world</View>

总结

大括号或者元素标签必须跟return在同一行,如果用了大括号则里面的元素标签可任意换行

猜你喜欢

转载自blog.csdn.net/taoerchun/article/details/91952357