开发者社区> 问答> 正文

,Warning: Each child in an array or iter?报错

@vczero 你好,想跟你请教2个问题:

问题1:

React-Native报错和警告

总是报这个警告:Warning: Each child in an array or iterator should have a unique "key" prop. Check the...


下面是我根据您编写的《React Native 入门与实战》这本书上的一句一句敲的代码,请问这个怎么解决?

var List = React.createClass({
	getInitialState: function(){
		return{
			count: 0
		};
	},

	componentDidMount: function(){
		var _that = this;
		AsyncStorage.getAllKeys(function(err,keys){
			if (err) {

			}
			_that.setState({
				count: keys.length
			});
		});
	},

	render: function(){
		var list = [];
		for (var i in Model) {
			if (i % 2 === 0) {
				var row = (
					<View style = {styles.row}>
						<Item  
							url = {Model[i].url}
							title = {Model[i].title}
							press = {this.press.bind(this, Model[i])}>
						</Item>

						<Item 
							url = {Model[parseInt(i)+1].url}
							title = {Model[parseInt(i)+1].title}
							press = {this.press.bind(this, Model[parseInt(i)+1])}>
						</Item>
					</View>
				);
				list.push(row);
			}
		}

		var count = this.state.count;
		var str = null;
		if (count) {
			str = ', 共' + count + '件商品';
		}

		return(
			<ScrollView style = {{marginTop:10}}>
				{list}
				<Text onPress = {this.goPay} style = {styles.btn}>去结算{str}</Text>
			</ScrollView>
		);
	},

	goPay:function(){
		this.props.navigator.push({
			component: TestCtrl,
			title: '购物车'
		});
	},

	press: function(data){
		var count = this.state.count;
		count++;
		this.setState({
			count:count
		});

		AsyncStorage.setItem('SP-'+this.genId()+'-SP', JSON.stringify(data),function(err){
			if (err) {

			}
		});
	},

	genId:function(){
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(c){
			var r = Math.random() * 16 | 0,
			v = c == 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		}).toUpperCase();
	},
});



问题2:

还有一个问题就是,我一个导航栏push到第三层就不能继续往下push了,报错udefined is not an object(evaluating 'this.props.navigator.push')

报错截图如下:

展开
收起
爱吃鱼的程序员 2020-06-09 16:05:49 506 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    循环的时候加个key={i}虽然并没啥用回复<aclass='referer'target='_blank'>@zxiaowei:<Viewkey={i}style={styles.row}></View>加在哪里?http://stackoverflow.com/questions/34576332/warning-each-child-in-an-array-or-iterator-should-have-a-unique-key-prop-che看上面那个链接,就有解决方案

    2020-06-09 16:06:05
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Tracking-Ransomware-End-To-End 立即下载
Nobody puts Spark in the Container 立即下载
低代码开发师(初级)实战教程 立即下载