Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应本机、意外的关键字“;这";_Javascript_Html_Reactjs_React Native_Jsx - Fatal编程技术网

Javascript 反应本机、意外的关键字“;这";

Javascript 反应本机、意外的关键字“;这";,javascript,html,reactjs,react-native,jsx,Javascript,Html,Reactjs,React Native,Jsx,为什么我有一个红色的错误标记这个关键字后的点?上面写着(意料之外的关键字“This”) render(){ 返回( this.selectionOnPress({this.props.detail.country}}> {this.props.detail.country} ); }} 试试这个 render() { return ( <TouchableOpacity onPress={() => this.selectionOnPress(this.pr

为什么我有一个红色的错误标记这个关键字后的点?上面写着(意料之外的关键字“This”)

render(){
返回(
this.selectionOnPress({this.props.detail.country}}>
{this.props.detail.country}
);
}}
试试这个

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>

    );
}}
render(){
返回(
this.selection不按(this.props.detail.country)}>
{this.props.detail.country}
);
}}
我基本上从第一个和第二个
this.props.detail.country的实例中删除了周围的花括号
在JS中,这些花括号是对象表示法,缺少属性名。

第三个实例是JSX模板变量。

当您将鼠标悬停在它上面时,它会说什么?@Nathan它会说(意外的关键字“This”),它不会像以前那样在红色和灰色之间切换。那么我猜是SelectionNopress()更新状态。selectedButton?如果state.selectedButton的初始值为falsy,则第一次点击会将其设置为props.detail.country,并将按钮背景更改为红色。现在,如果你再次点击它,会发生什么?还有另一个按钮可以将state.selectedButton设置为不同的值吗?构造函数(){super();this.state={selectedButton:null};this.selectionOnPress=this.selectionOnPress.bind(this);}selectionOnPress(userType){this.setState({selectedButton:userType});},在
selectionOnPress()
中,执行以下操作:
this.setState(preState=>prevState.selectedButton===userType?null:userType)
这将在null和点击同一按钮时的值之间切换,并应在红色和灰色按钮之间切换。即使我点击两次,也只显示灰色,不再显示红色。这个解决方案不起作用。
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>

    );
}}