cannot add a child that doesnot have a YogoNode to a parent without a measure function!

来源:互联网 发布:上海linux运维培训 编辑:程序博客网 时间:2024/04/29 17:19

最近在学习rn,各种报错和坑,而且对语法也不熟悉,为啥要用{}这种形式呢?唉,还是我大java好,不扯淡了。


在使用FlatList的时候,老是报

cannot add a child that doesnot have a YogoNode to a parent without a measure function!(Trying to add a 'ReactRawTextShadowNode' to a 'LayoutShadowNode')

这个错,然后我反复找我的代码,是真的没有错,代码如下

import React, {Component} from 'react';import {StyleSheet,View,FlatList,Text,Button,Dimensions} from 'react-native';const {width,height}=Dimensions.get('window')export default class FlatDemo extends Component{        render(){        return(            <View style={styles.container}>                <FlatList                     data={[                        {key:'haha1'},                        {key:'haha2'},                        {key:'hah3'},                        {key:'heihei4'},                        {key:'john'},                        {key:'aaa'},                        {key:'bbb'},                        {key:'ccc'},                        {key:'ddd'}                    ]}                    renderItem={({item})=><Text style={styles.item}>{item.key}</Text>}                />            </View>        );    }}const styles=StyleSheet.create({    container:{        flex:1,        paddingTop:120    },    item: {    padding: 10,    fontSize: 18,    height: 44,  }})
特么的,这哪里有错啊,就当我百思不得其解的时候,我决定把这个文件去掉,直接写在App.js中

import React, {  Component} from 'react';import {  Platform,  StyleSheet,  Text,  View,  FlatList} from 'react-native';export default class App extends Component{  render(){    var params = {name:'小明',age:29,sex:'女'};    var {name,age}=params;    return(        <View style={styles.container}>                      <FlatList              data={[                  {key:'嘿嘿'},                  {key:'嘿嘿2'},                  {key:'嘿嘿3'},                  {key:'嘿嘿4'},                  {key:'嘿嘿5'},                  {key:'嘿嘿6'},                ]}                renderItem={({item})=><Text style={styles.item}>{item.key}</Text>}             />        </View>    );  }}const styles=StyleSheet.create({    container:{        flex:1,        paddingTop:20    },  item: {    padding: 10,    fontSize: 18,    height: 44,  }})
不幸的是,还是报同样的错,然后各种google,baidu还是没有结果,忘了说了,我当前rn版本是0.50,我就想把根view去掉试试

import React, {  Component} from 'react';import {  Platform,  StyleSheet,  Text,  View,  FlatList} from 'react-native';export default class App extends Component{  render(){    var params = {name:'小明',age:29,sex:'女'};    var {name,age}=params;    return(            <FlatList style={styles.container}              data={[                  {key:'嘿嘿'},                  {key:'嘿嘿2'},                  {key:'嘿嘿3'},                  {key:'嘿嘿4'},                  {key:'嘿嘿5'},                  {key:'嘿嘿6'},                ]}                renderItem={({item})=><Text style={styles.item}>{item.key}</Text>}             />            );  }}const styles=StyleSheet.create({    container:{        flex:1,        paddingTop:20    },  item: {    padding: 10,    fontSize: 18,    height: 44,  }})
这样就成功了,卧槽,这样搞得话,岂不是一个页面只能使用FlatList,难道就不能和其他view一起用了?

后续更新中.......小伙伴要是有碰到的,已经解决的给我留言啊!不胜感激。

2017/12/5更新

不要在你的flatList里面使用注释

Some times you need to remove the extra spaces between the angle brackets to get rid of it.这是facebook官方人员回复的,希望对大家有帮助!!!

2017/12/7更新

不要在你的render里的return里的根view里面使用注释


原创粉丝点击