![](/img/trans.png)
[英]map function in javascript not working properly for React components
[英]Javascript | React Native map function not working properly
因此,我在使用React Native时发现了一个非常愚蠢的问题,似乎无法解决。 我已经声明了具有一些硬编码值的对象数组(此处为storeVar)。 当我尝试使用javascript映射函数遍历它时,仅在第一个索引处才得到该值。 代码如下-
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
ScrollView,
AsyncStorage,
TouchableOpacity
} from 'react-native';
import Note from './note.js'
// -------------------------------------------------------------------------
const storeVar = [
{
'd' : '',
'n' : 'Hello'
},
{
'd' : '',
'n' : 'Awesome'
},
];
export default class reactNativePractise extends Component {
constructor(props) {
super(props);
this.state = {
noteArray : [
{
'date' : '',
'note' : ''
}
],
};
}
componentDidMount() { this.onLoad(); }
onLoad = async()=> {
storeVar.map(async(value, index) => {
try {
var d = new Date();
// const storedVal = [await AsyncStorage.getItem(storeVar[index].n)];
alert("Key is : " + JSON.stringify(storeVar[index-1].n, null, 4));
// this.state.noteArray.push( {date :d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate(), note : storedVal[index]} );
}
catch(error) { alert('Error' + error) }
});
}
仅显示Hello,而不显示Awesome。 任何帮助将不胜感激。
不要使用index-1
,而要使用
JSON.stringify(storeVar[index].n, null, 4));
该索引从开始0
,这样做-1
的0
将导致到-1
,并且将无法访问的-1th元件array
,当索引变为1
和1 - 1
将是0
当时其打印的Hello( 0th
索引元素)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.