[英]When i export my array, i can only list the last item of my array - React Native
目前我正在制作一個單詞測試應用程序。 我找到了一個數據庫並在我的數組中添加了所有項目。
這是我的代碼:
import React from 'react';
import Navigator from './routes/menuStack';
export const N5Words = [];
export default function App(){
var RNFS = require('react-native-fs');
RNFS.readFileAssets('N5.txt', 'utf8')
.then((contents) => {
let allWords = contents.split('\n');
let word = { key: 0, japanese: 'japanese', kana: 'kana', turkish: 'turkish' }
for (var i = 0; i < allWords.length; i++){
let splittedRow = allWords[i].split('\t');
word.key = i;
word.japanese = splittedRow[0].toString();
word.kana = splittedRow[1].toString();
word.turkish = splittedRow[2].toString();
N5Words.push(word);
}
})
return(
<Navigator/>
);
}
我想在另一個腳本中使用這個數組。 所以,我在另一個腳本上寫了下面的代碼:
import React from 'react';
import {StyleSheet, View, Text, Button, ScrollView} from 'react-native';
import { globalStyles } from '../styles/global';
import { N5Words } from '../App';
export default function KelimeTesti({ navigation }){
return (
<View style={globalStyles.container}>
<Text style={globalStyles.smallText}>You're in Kelime Testi</Text>
<ScrollView>
{ N5Words.map((word) => {
return (
<View key={word.key}>
<Text style={globalStyles.smallText}>{word.japanese}</Text>
</View>
)
})}
</ScrollView>
</View>
)
}
但它不起作用。 當我導出數組並在另一個腳本中使用該數組時,只列出了我的腳本的最后一項。
第一段代碼沒有問題。 我檢查了控制台中的項目,它正在正確讀取所有單詞,並且它們具有從 0 到 668 的唯一 ID。
我收到此錯誤 668 次(對於每個單詞):
這就是我的數據庫的樣子:
會う あう to meet
青 あお blue
青い あおい blue
赤 あか red
赤い あかい red
明い あかるい bright
秋 あき autumn
開く あく to open,to become open
開ける あける to open
上げる あげる to give
朝 あさ morning
我解決了這個問題。 我正在為任何處理這個問題的人寫這篇文章。 而不是導出和導入數據庫數組,我將它與導航參數一起發送。 我用 getParam 方法得到數組。 然后它起作用了。
在列出數組時,我同時使用了 map() 函數和平面列表。 我無法列出項目,因為我收到了“未定義不是對象”的錯誤。 我也解決了這個問題。 我在我的問題中使用了一個名為“word”的變量。 我把它改成了“項目”,就是這樣。 我認為我們不能在列出數組時使用除“item”之外的另一個變量名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.