首页 > 解决方案 > React Native,意外的关键字“This”

问题描述

为什么我有一个红色错误标记这个关键字后面的点?它说(意外的关键字“这个”)

    render() {
    return (
        <TouchableOpacity onPress={() => this.selectionOnPress({this.props.detail.country})}>
            <Text style={[styles.btnSV, {
                backgroundColor:
                    this.state.selectedButton === {this.props.detail.country} ? "red" : "grey"
            }]}>
                <Text style={styles.btnSV}>{this.props.detail.country}</Text>
            </Text>
        </TouchableOpacity>

    );
}}

错误图片

标签: javascripthtmlreactjsreact-nativejsx

解决方案


尝试这个

render() {
    return (
        <TouchableOpacity onPress={() => this.selectionOnPress(this.props.detail.country)}>
            <Text style={[styles.btnSV, {
                backgroundColor:
                    this.state.selectedButton === this.props.detail.country ? "red" : "grey"
            }]}>
                <Text style={styles.btnSV}>{this.props.detail.country}</Text>
            </Text>
        </TouchableOpacity>

    );
}}

我基本上从 Within JS 的第一个和第二个实例中删除了周围的大括号this.props.detail.country ,这些大括号是对象表示法并且缺少属性名称。第三个实例是 JSX 模板变量。


推荐阅读