VK1
VK1

Reputation: 1726

How do I use AsynchStorage to load data when an app starts..making all loaded data accessible across the platform?

I want to use AsynchStorage.setItem() to load two different objects that contain phrases that will be rendered dynamically depending on which language is picked.

Would creating a completely separate component(loadLanguages.js), then importing it to the splash page, allow me to access the objects across all pages. After state(language) has been set, I want to dynamically render all pages using the objects I originally loaded on the splash page.

So far this is what my loadLanguages.js file looks like so far

import React,{Component} from 'react';
import {AsyncStorage, View} from 'react-native';

    export default class loadLanguages extends Component{
        componentDidMount(){
            let objFr = {
                name: 'nom',
                location: 'emplacement',
            };

            let objEn = {
                name: 'name',
                location: 'location',

            };

            AsyncStorage.setItem("french", JSON.stringify(objFr));
            AsyncStorage.setItem("english", JSON.stringify(objEn));

        }
        // render(){
        //     return(<View/>);
        // }
    }

I wasn't sure if i need to return a View or not.

I've imported this loadLanguages.js to the splash page and another page and tried to access the object via AsynchStorage.getItem('french') but I keep getting a null as a response.

Upvotes: 0

Views: 92

Answers (1)

VK1
VK1

Reputation: 1726

I did not need to wrap the objects into any components.

import {AsyncStorage} from 'react-native';

let objFr = {
    name: 'nom',
    location: 'emplacement',
};

let objEn = {
    name: 'name',
    location: 'location',

};

AsyncStorage.setItem("french", JSON.stringify(objFr));
AsyncStorage.setItem("english", JSON.stringify(objEn));

Upvotes: 1

Related Questions