代码之家  ›  专栏  ›  技术社区  ›  swonder

不变冲突:文本字符串必须在<Text>组件中呈现

  •  10
  • swonder  · 技术社区  · 8 年前

    我利用了他们的错误功能 TextInput 这些组件基本上启用了可以设置错误消息样式和设置错误消息的道具。非常方便,但是升级破坏了这些,我现在遇到了这个错误:

    Invariant Violation: Text strings must be rendered within a <Text> Component.

    { this.state.event.cards[i].fields[j].error && 
    
      <Text style={{ color: '#e74c3c', fontSize: 14, paddingLeft: 5 }}>
        {this.state.event.cards[i].fields[j].error}
      </Text>
    }
    

    一旦 this.state.event.cards[i].fields[j].error 变成一个字符串,我得到返回这个错误。但是你可以看到我检查错误是否存在,然后我只是显示错误,或者至少尝试。

    5 回复  |  直到 8 年前
        1
  •  55
  •   Yuci    7 年前

    this.state.error === undefined

    render() {
      return (
        <View>
          {this.state.error &&
    
            <Text>
              Error message: {this.state.error}
            </Text>
          }
        </View>
      );
    }
    

    如果错误状态更改为空字符串“”,则会出现上述异常: Invariant Violation: Text strings must be rendered within a <Text> component

    原因是 this.state.error === '' ,以下表达式将被计算为空字符串,即“”,这将导致

    {this.state.error &&
    
      <Text>
        Error message: {this.state.error}
      </Text>
    }
    

    什么时候? this.state.error错误===未定义 undefined ,这正是我们所期望的,而且很好。

        2
  •  60
  •   Tare Gaskin    7 年前

        3
  •  57
  •   sed    5 年前

    每当你看到

    Invariant Violation: Text strings must be rendered within a <Text> component
    

    99%的情况将由使用仅带&而不是三元的条件呈现引起?:语句。为什么?因为当你的条件为null或未定义时,那里没有任何组件,而空数组会显示一个空的空间,并从地狱的红屏中拯救你的生命。

    将所有逻辑条件呈现更改为三值呈现。

    即:

    不要这样做

    widgetNumber === 10 && <MyComponent />
    

    做,做

    widgetNumber === 10 ? <MyComponent /> : null
    

        4
  •  33
  •   Otani Shuzo    7 年前

    我会用 !! error . 这应该管用。

    
    {!!this.state.error && (
      <Text>
        Error message: {this.state.error}
      </Text>
    )}
    

    如果 错误 是一个字符串,不管文本应该用什么包装 <Text /> 正如React Native中的错误消息所述,这与web不同。

        5
  •  6
  •   Lucas Falcão    6 年前

    使用语法 {condition && <Element/>}

    问题的根源是逻辑短路。在Javascript中,逻辑表达式计算 && 计算到最后一个 true 等效值。但不是为了 是的 ,但在将其转换为布尔值之前将其转换为实际值。这才是真正允许这种语法的开始。自 <Element/> 总是 如果 condition 是的 <

    当情况恶化时,问题就出现了 false . 当 && 表达式失败时,它将计算为第一个值,即 所以,如果使用直接字符串或数字作为 条件 它的计算结果是 假 逻辑表达式将计算为该字符串或数字。 前任。: {array.length && <List/>} 使用空数组计算为 {0} 它抛出了错误。

    解决办法是确保 条件 {array.length > 0 && <List/>}

        6
  •  3
  •   Ben Butterworth    6 年前

    对我来说,这是因为JSX中一个未打开的花括号。

    <View>
            {events.map(event => {
              return (
                <Card title={event.summary}>
                  <Text>Date: {event.start.dateTime}</Text>
                </Card>
              );
            })}
            } <------------ that was the culprit
    </View>
    
        7
  •  3
  •   SilverTech    6 年前

    从阅读这里的其他答案来看,这个错误消息是含糊不清的,并显示了各种原因。我在写评论的时候,甚至是在视图中写一个小的空白(疯狂的是…)

    <View> {props.children} </View>

    必须更改为: <View>{props.children}</View>

        8
  •  2
  •   Melih Mucuk    8 年前

    这通常发生在执行内联条件呈现时。你应该删除两行之间的空白 Text 你的情况如下。

    { this.state.event.cards[i].fields[j].error && <Text style={{ color: '#e74c3c', fontSize: 14, paddingLeft: 5 }}>
        {this.state.event.cards[i].fields[j].error}
      </Text>
    }
    
        9
  •  2
  •   Sarthak Sharma    6 年前

    在我的例子中,这个错误是由于在结束Text标记时出现分号“;”。对于那些没有得到上述正确答案的人,试试这个。去掉分号,错误就消失了。

        10
  •  1
  •   hksande    7 年前

    我在VSCode中遇到了相同的错误消息,但我没有/*注释*/或任何表达式。 解决方案是删除textedit或word中的格式,然后将代码复制并粘贴回vscode。

        11
  •  1
  •   MD. IBRAHIM KHALIL TANIM    6 年前

    如果初始值是空字符串,请使用下面给出的代码。或有条件地检查初始值。这里我有条件地检查错误是否为空,然后显示错误。

    {this.state.error !== '' && (
        <Text>
          {this.state.error}
        </Text>
    )}
    
        12
  •  0
  •   Naved Khan    7 年前
     <React.Fragment>
          {this.props.title ? (
            <React.Fragment> 
              <Text>  true </Text>
            </React.Fragment>
            ):(             
               <React.Fragment> 
              <Text>  false </Text>
              <Text>  false </Text>
            </React.Fragment>
    

    查看 标记或 反应。碎片 如果元素多于一个,则标记和内部也需要换行

        13
  •  0
  •   Emad Deym    7 年前

    试试这个:

    <>
        <View>
        {this.state.error &&
          <Text>
            Error message: {this.state.error}
        </Text>
       </View>
    </>
    
        14
  •  0
  •   U13-Forward    6 年前
    {this.state.password.length > 0 && <Text>}
    

    这将抛出相同的错误,因为它返回undefined。 我们可以这样渲染-

    {this.state.password.length > ? <Text> : null}
    
        15
  •  0
  •   Teju    6 年前
    In my case some part of the code was out side the text component for example:
    <RadioForm style={styles.radiobutton} 
                          radio_props={hobbies}
                          initial={1}
                          onPress={(value) => {ToastAndroid.show(value.toString(), ToastAndroid.SHORT)}}
                          buttonSize={4.5}
                          buttonOuterSize={19}
                          selectedButtonColor={'green'}
                          selectedLabelColor={'green'}
                          labelStyle={{ fontSize: 14, }}
                          disabled={false}
                          formHorizontal={true}
                 />
    <RadioForm
    

        16
  •  0
  •   DiaMaBo    6 年前

    我也遇到了同样的问题,错误是在children和{children}等组件之间添加了空格

    注意 :删除{children}附近的任何空格

     <Provider value={something}>{children}</BlogContext.Provider>
    
        17
  •  0
  •   Eric Wiener    6 年前

    export const p = (props) => {
      return (
        <Text style={[styles.p]}>{props.children}</Text>
      );
    };
    

    应该是:

    export const P = (props) => {
      return (
        <Text style={[styles.p]}>{props.children}</Text>
      );
    };
    

    注意第二个 p

        18
  •  0
  •   Bulent Balci    6 年前

    我也有同样的问题, 在我的情况下,我有一个 <AppText> 显示简单格式的组件 <Text>

    <AppText> </AppText>

    <AppText />

    然后问题突然解决了。

    我想这只是一个特殊的&我错在标签之间写了一个看不见的字符 或者这个字符是通过自动完成功能或代码片段从VSCode来的,

        19
  •  0
  •   ATQSHL    6 年前

    在我的例子中,发生这种情况是因为render函数中的注释

    // Some comment here
    

    解决:

    • 或者像这样 {/* Same comment here*/}
        20
  •  0
  •   Richard Torcato    6 年前

    有时,Prettier或您的代码格式化程序会在保存时将其添加到JSX中。

    <View>
    {' '}
    </View>
    

    去掉这个你就没事了。

        21
  •  0
  •   Red    6 年前

    此错误发生在 /*Commented code*/ 在你的 return() 功能。

    为了解决这个问题,我们花了一整天的时间做完全无关的事情。

        22
  •  0
  •   karan bhatia    5 年前

    有接线员!!它将字符串类型转换为布尔值,并在变量为空字符串时使其为false:

    {!!error &&
      <Text>
        Error message: {error}
    </Text>}
    
        23
  •  0
  •   Deepak    5 年前

    > 可触摸不透明度 React native touchable Opacity .

    错误代码

    <TouchableOpacity>
            onPress={() => this.setState({ showOverLay: true })}
              <Image
                source={cardImage}
                resizeMode="contain"
                style={[
                  styles.cardImage, cardImageStyle
                ]}
              />
    </TouchableOpacity>
    

    好的代码 :

      <TouchableOpacity
            onPress={() => this.setState({ showOverLay: true })}>
              <Image
                source={cardImage}
                resizeMode="contain"
                style={[
                  styles.cardImage, cardImageStyle
                ]}
              />
    </TouchableOpacity>
    
        24
  •  0
  •   Abdes    5 年前

    从

    <View>  <Text style={{ color: "white"}}>Start</Text> </View>
    

    到

    <View><Text style={{ color: "white"}}>Start</Text></View>
    
        25
  •  0
  •   Sanjib Debnath    5 年前

    下面的代码还检查空字符串和truthy值。

      return(
        <View>
        {!!this.state.error && <Text>{this.state.errorMessage}</Text>}
        </View>
      );
    
        26
  •  0
  •   za_ali33    5 年前

    对我来说,这个错误是因为我将state对象初始化为空字符串。

    const [category, setCategory] = useState("")
    

    const [category, setCategory] = useState(null)
    

        27
  •  -1
  •   gehbiszumeis Anja H    6 年前
    this.state.recording ?(<Text>{this.secondsToMMSS(this.state.seconds)}</Text>) :                                     (null)
    

        28
  •  -2
  •   valn1 XD    5 年前

    好吧,如果你走这么远,那是因为你可能和我有同样的问题,例如:

    返回{行}

        29
  •  -4
  •   Kalon Acharjee    7 年前

    推荐文章