React Native 三元运算符
React Native Ternary Operator
我正在尝试使用三元运算符截断用户名并在超过特定字符长度时添加 ...
。我已经尝试了几种方法,但一直回到这个问题,下面是原始代码和我现在拥有的代码。
<Text style={styles.userText}>{name}</Text>
<Text style={styles.userText}>
{name.length > 27 ? name.substr(0, 27)`...` : name} -
</Text>
我在模拟器中收到的错误是“name.substr(0, 27) 不是一个函数”,我知道但不确定为什么 returns。
感谢任何反馈和帮助。
感谢@OmkarKulkarni 的评论,解决方案如下:
<Text style={styles.caregiverText}>
{name.length > 27 ? `${name.substr(0, 27)}...` : name} -
</Text>
三元运算符应该与字符串文字一起使用。这会起作用。
<Text style={styles.caregiverText}>
{name.length > 27 ? `${name.substr(0, 27)}...` : name} -
</Text>
我正在尝试使用三元运算符截断用户名并在超过特定字符长度时添加 ...
。我已经尝试了几种方法,但一直回到这个问题,下面是原始代码和我现在拥有的代码。
<Text style={styles.userText}>{name}</Text>
<Text style={styles.userText}>
{name.length > 27 ? name.substr(0, 27)`...` : name} -
</Text>
我在模拟器中收到的错误是“name.substr(0, 27) 不是一个函数”,我知道但不确定为什么 returns。
感谢任何反馈和帮助。
感谢@OmkarKulkarni 的评论,解决方案如下:
<Text style={styles.caregiverText}>
{name.length > 27 ? `${name.substr(0, 27)}...` : name} -
</Text>
三元运算符应该与字符串文字一起使用。这会起作用。
<Text style={styles.caregiverText}>
{name.length > 27 ? `${name.substr(0, 27)}...` : name} -
</Text>