Javascript 反应本机、意外的关键字“;这";
为什么我有一个红色的错误标记这个关键字后的点?上面写着(意料之外的关键字“This”)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
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>
);
}}