【发布时间】:2020-04-22 21:27:40
【问题描述】:
我正在尝试使用 array.map,但我无法看到结果。请帮忙。
我正在尝试创建一个类似单元格的日历,下面的代码不会显示任何错误或任何内容。
只需遍历两个数组即可获得结果。但它不会工作。我是 React Native 初学者
import React, { Component } from 'react';
import { View, StyleSheet, Text } from 'react-native';
export default class LearnMap extends React.Component {
constructor(props) {
super(props);
}
render() {
var rows = [0,1,2,3,4,5,6];
var cols = [
['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'],
[1,2,3,4,5,6,7],
[8,9,10,11,12,13,14],
[15,16,17,18,19,20,21],
[22,23,24,25,26,27,28],
[29,30,31,1,2,3,4],
[5,6,7,8,9,10,11],
];
return(<View style={{flex:1,justifyContent:'center', alignItems:'center'}}>
{ rows.map((row, rowIndex)=>{
<View key={rowIndex} style={styles.row}>
{ cols[row].map((col, colIndex)=>{
<View key={colIndex} style={styles.cell}><Text key={colIndex}>{col.toString}</Text></View>
})
}
</View>
})
}
</View> );
}
}
const styles = StyleSheet.create({
container: {
},
row:{
flexDirection:'row',
marginLeft:10,
marginRight:10,
},
cell:
{
flex:1,
backgroundColor:'pink',
alignItems:'center',
justifyContent:'center',
height:45,
borderWidth:1, borderColor:'black'
}
});
【问题讨论】:
-
你在
rows.map()忘记了return