【问题标题】:I am trying to use conditional statement in Text in TouchableObecity in ReactNative我正在尝试在 React Native 的 TouchableOpacity 中的 Text 中使用条件语句
【发布时间】:2022-08-14 15:24:21
【问题描述】:

我正在尝试在 React Native 的 TouchableOpacity 中的 Text 中使用条件语句 但屏幕上没有显示任何内容。该文件是 .tsx

这是我的代码 `

let count=1
<TouchableOpacity
                                    onPress={() => {}}
                                >

                                    <Text style={{
                                        // paddingLeft: 8,
                                        width:\'100%\',
              
                                    }}>
                                        {()=> {
                                            if (count==1) {
                                                <Text>
                                                View Order
                                            </Text>
                                            } else if (count==2) {
                                                <Text>
                                                    View Product
                                                </Text>
                                            }
                                        }} 
                                    </Text>
                                </TouchableOpacity>

`

    标签: react-native


    【解决方案1】:

    尝试使用这样的简单形式:

    let count=1
    <TouchableOpacity
        onPress={() => {}}>
        <Text style={{
              // paddingLeft: 8,
              width:'100%',
          }}>
    
          {count === 1 ? <Text>View Order</Text> : count === 2? <Text> View 
    Product </Text> : <Text>Third command</Text>}
                                             
        </Text>
    </TouchableOpacity>
    

    添加更多条件:在“:”之后使用条件

    【讨论】:

    • 我想添加另一个条件 if() 我该怎么做?
    【解决方案2】:

    const render_txt = () => {
      let count = 1;
      switch(count) {
      case 1:
        return 'every thing you want 1';
      case 2:
        return 'every thing you want 2';
      default:
        // code block
        break;
      }
    }
    <TouchableOpacity onPress={() => {}}>
      <Text style={{ paddingLeft:8, width:'100%'}}>
        {render_txt()} 
      </Text>
    </TouchableOpacity>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多