简体   繁体   中英

When i export my array, i can only list the last item of my array - React Native

Currently i'm making a word test application. I found a database and added all items in my array.

Here is my code:

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/>
  );
}

I want to use this array in another scripts. So, i write the code below, on the other script:

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>
    )
}

But it's not working. When i export the array and use that array in another script, only the last item of my script is listed.

Output of the Code

There is no problem with the first code. I checked the items in console and it's reading all the words properly and they have unique id's which is from 0 to 668.

I get this error 668 times (for every word):

Error Image

And that's how my database looks like :

会う  あう  to meet
青   あお  blue
青い  あおい blue
赤   あか  red
赤い  あかい red
明い  あかるい    bright
秋   あき  autumn
開く  あく  to open,to become open
開ける あける to open
上げる あげる to give
朝   あさ  morning

I solved the problem. I'm writing this for anyone who deals with this problem. Instead exporting and importing the database array, i sent it with navigation parameter. And i get the array with getParam method. Then it worked.

While listing the array, i used both map() function and flat list. I wasn't able to list the items because i was getting an error which is "undefined is not an object". I also solved that problem. I was using a variable called "word" as in my question. I changed it into "item" and that's it. I think we can't use another variable name except "item" while listing array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM