Javascript React native:在呈现(返回)中使用条件
我试图向用户显示一些消息,如果某些值是变量 我有这样一个代码:Javascript React native:在呈现(返回)中使用条件,javascript,react-native,Javascript,React Native,我试图向用户显示一些消息,如果某些值是变量 我有这样一个代码: connection(device, side) { //.... if(side == "right"){ this.setState({deviceRight: true}) } } renderConnection(connectionProgress){ switch(connectionProgress) { case 1: if(this.state.deviceRight ==
connection(device, side) {
//....
if(side == "right"){
this.setState({deviceRight: true})
}
}
renderConnection(connectionProgress){
switch(connectionProgress) {
case 1:
if(this.state.deviceRight == true){
return(<View><Text>Device Right Connected</Text></View>)
}
default:
return null;
}
render() {
const {connectionProgress} = this.state
return (
<Text>Waiting for connection..</Text>
{this.renderConnection(connectionProgress)}
)
}
}
连接(设备,侧面){
//....
如果(侧面=“右侧”){
this.setState({deviceRight:true})
}
}
渲染连接(连接进度){
交换机(连接进度){
案例1:
if(this.state.deviceRight==true){
返回(设备右连接)
}
违约:
返回null;
}
render(){
const{connectionProgress}=this.state
返回(
正在等待连接。。
{this.renderConnection(connectionProgress)}
)
}
}
因此,我希望在变量(例如deviceRight
设置为“true”)时显示文本
我该怎么办?我用这种方式解决了:
connection(device, side) {
//....
if(side == "right"){
this.setState({deviceRight: true})
}
}
renderConnection(){
if(this.state.deviceRight == true)
{
return <Text>Device Connected </Text>
}
else {
return <Text> Device not Connected</Text>}
}
render() {
return (
<Text>Waiting for connection..</Text>
{this.renderConnection()}
)
}
}
连接(设备,侧面){
//....
如果(侧面=“右侧”){
this.setState({deviceRight:true})
}
}
renderConnection(){
if(this.state.deviceRight==true)
{
返回设备已连接
}
否则{
返回设备未连接}
}
render(){
返回(
正在等待连接。。
{this.renderConnection()}
)
}
}
在这种情况下,您可以使用三元运算符,如下所示-
render() {
return (
<View>
<Text>Waiting for connection..</Text>
{this.state.deviceRight ? (
<Text>Device Connected </Text>
) : (
<Text> Device not Connected</Text>
)}
</View>
)
}
render(){
返回(
正在等待连接。。
{this.state.deviceRight(
设备连接
) : (
设备未连接
)}
)
}
您会遇到什么错误?基本上,我在GTE开关盒中编写的内容不会显示绑定renderConnection
,例如renderConnection=(connectionProgress)=>
。我尝试过,但没有显示任何内容。。