如何解决,错误是“永久违反:文本字符串必须在<Text>组件内呈现。”

Vipin Katara

我试图使config()运行,但是它以某种方式失败了。

import React, {Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default class try1 extends Component {

  constructor(noofVertices){
    super();
    this.noofVertices = this.noofVertices
    this.edge = {}
this.a = [];}


addVertex(v){
  this.edge[v] = {}
}

addEdge(v, w,weight){
  if (weight == undefined) {
           weight = 0;
       }
       this.edge[v][w] = weight;
  }

config(){

    var vertices = [ 'App0', 'App1', 'App2', 'App3', 'App4' ];

    // adding vertices
    for (var i = 0; i < vertices.length; i++) {
      this.addVertex(vertices[i]);
    }

    // adding edges
    this.addEdge('App0', 'App1',1);
    this.addEdge('App0', 'App2',1);
    this.addEdge('App0', 'App3',1);
    this.addEdge('App0', 'App4',1);

    this.addEdge('App1', 'App0',1);
    this.addEdge('App1', 'App2',1);
    this.addEdge('App1', 'App3',1);
    this.addEdge('App1', 'App4',1);

    this.addEdge('App2', 'App0',1);
    this.addEdge('App2', 'App1',1);
    this.addEdge('App2', 'App3',1);
    this.addEdge('App2', 'App4',1);


    this.addEdge('App3', 'App0',1);
    this.addEdge('App3', 'App1',1);
    this.addEdge('App3', 'App2',1);
    this.addEdge('App3', 'App4',1);

    this.addEdge('App4', 'App0',1);
    this.addEdge('App4', 'App1',1);
    this.addEdge('App4', 'App2',1);
    this.addEdge('App4', 'App3',1);


    this.traverse('App1');
    } 



traverse(vertex)


 {

            for(var adj in this.edge[vertex])
                this.a.push(this.edge[vertex][adj])
        this.a.sort()


           //this.updateEdge1('App0');   
        }




render(){

  return (
    <View style={styles.container}>

      this.config()
      <Text>{this.a}</Text>

    </View>
  );
}
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    alignItems: 'center',
    justifyContent: 'center',
  },

}
);

我希望11111将显示在屏幕上。

它显示错误“不变违例:文本字符串必须在组件内呈现”。

我正在尝试处理图形,我也尝试过地图,但这没有用。

反应本地支持地图吗?

内森

请注意您this.a的财产try1组件是一个数组:this.a = [];和你想你的内直接显示该物业的render()方法,像这样:<Text>{this.a}</Text>但是,由于以下原因,这会导致问题:

render()方法不支持直接呈现仅数组的返回类型调用React组件的render()方法时,它必须返回以下之一:

  1. 反应元素通常通过JSX创建例如,<div /><MyComponent />是React元素,分别指示React渲染DOM节点或另一个用户定义的组件。
  2. 数组和片段让您从渲染返回多个元素。有关更多详细信息,请参见有关片段的文档。
  3. 门户网站让您将子级渲染到另一个DOM子树中。有关更多详细信息,请参见门户网站上的文档。
  4. 字符串和数字。这些在DOM中呈现为文本节点。布尔值或null。什么都不渲染。(大多数情况下都支持返回测试&&<Child />模式,其中test为布尔值。)

有关更多信息,请查看render() 规范

还有其他错误在此代码为S,以及:

  1. 自定义react组件必须以大写字母命名,否则JSX会认为这是HTML标记。重命名try1Try1
  2. 将配置函数调用移到return语句之前,因为return语句期望返回实际的视图本身

考虑到这些要点,请尝试遍历this.a并为数组中的每个Text元素提供要显示元素,如下所示:

render() {
    this.config();
    let aEles = this.a;
    return(
        <View style={styles.container}>
            aEles.map(edge => (
               <Text>{edge}</Text>
            ));
        </View>

    )  
}

希望有帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试创建按钮时发生异常-永久违反:文本字符串必须在<Text>组件内呈现

递归:如何解决错误“不变违例:文本字符串必须在<Text>组件内呈现。”

React-Moment给出:“不变违反:文本字符串必须在<Text>组件内呈现”

组件异常:必须在<Text>组件内呈现文本字符串

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

为什么我的组件出现“错误:必须在<Text>组件内呈现文本字符串”?

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

文本字符串必须在ReactNativeRenderer-dev.js中的<Text>组件内呈现

解决不变式违规:文本字符串必须在<Text>组件中呈现

如何解决不变性冲突:必须在React Native的<Text>组件中呈现文本字符串?

RN错误:必须在<Text>组件中呈现文本字符串

错误:当字符串不为空时,文本字符串必须在 React Native 的 <Text> 组件中呈现

react-country-region-selector 组件给定错误 文本字符串必须在 <Text> 组件中呈现

StripeCheckout:文本字符串必须在 <Text> 组件中呈现

文本字符串必须在<Text>组件中呈现

React-Native, Invariant Violation:文本字符串必须在 <Text> 组件中呈现

条件逻辑反应原生“文本字符串必须在 <Text> 组件中呈现。”

文本字符串必须在 <Text> 组件中呈现

React Native Text 字符串必须在 <Text> 组件中呈现

“必须在<Text>中呈现文本”错误

React Native - 必须使用 <Text> 组件呈现文本

React Native 文本字符串必须在文本中呈现

如何在 blazor 中呈现剃刀组件内的内容?

必须呈现错误文本组件

如何更改呈现的组件?

如何在React Native的Text组件内输出<

路由呈现错误的组件,ReactJS

如何呈现动态组件onClick

您如何呈现组件html