【问题标题】:Invariant Violation: Text strings must be rendered within a componentInvariant Violation:文本字符串必须在组件内呈现
【发布时间】:2019-06-18 02:58:33
【问题描述】:

我正在尝试打印从 1 到 10 的数字,以便用户可以按下其中任何一个。 但是,我收到此错误,内容为

不变违规:文本字符串必须在 <Text> 内呈现 组件。

这是render里面的代码。

render(){
         let arr=[];
         for(let index=1;index<=this.state.max_rating;index++){
             arr.push(
                 <TouchableOpacity
                      activeOpacity={0.7}
                      key={index}
                      onPress={this.UpdateRating.bind(this, index)}>
                      <Text key={index}>
                        {'lol'}
                      </Text>
                   >
                </TouchableOpacity>
              );
         }

         return(
             <View style={styles.MainContainer}>
              <Text> The code is working man </Text>
              <View style={styles.childView}>{arr}</View>
             </View>
         );
     }

【问题讨论】:

标签: javascript reactjs react-native


【解决方案1】:

您的代码中存在轻微的语法问题

for(let index=1;index<=this.state.max_rating;index++){
         arr.push(
             <TouchableOpacity
                  activeOpacity={0.7}
                  key={index}
                  onPress={this.UpdateRating.bind(this, index)}>
                  <Text key={index}>
                    {'lol'}
                  </Text>
               > <-- extra character which is treated as a string giving you the warning
            </TouchableOpacity>
          );
     }

删除&lt;TouchableOpacity&gt;组件中呈现的&gt;字符,它会正常工作

【讨论】:

    猜你喜欢
    • 2019-07-01
    • 2019-02-20
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2019-02-21
    相关资源
    最近更新 更多