Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29601

How to view the changes of an imported file in React-Native?

$
0
0

I have a .js file that stores several json objects that will be used to dynamically create components in various parts of the app. For ex:

import langUtil from '../utilities/langUtil';
export default {
 ...
    DEFAULT_DAY_TIME: {
        "times": [
            {
                "name":langUtil.day_morning,
                "value":"1"
            },
            {
                "name":langUtil.day_afternoon,
                "value":"2"
            },
            {
                "name":langUtil.day_evening,
                "value":"3"
            }
        ],
    ...
}

langUtil uses the 'react-native-localization' package to give translations in various languages. Therefore, if the language is currently set to be English, it will show the text as "Morning" and if its French, "Matin" and so on. This actually works with no issue if I use langUtil.day_morning inside a component:

<Text>{langUtil.day_morning}</Text> //will show the correct language when language is changed

Because my app has an option to change language, I would like it to update all the text to the new language, and as mentioned, this works for when langUtil is used inside a component.

However, if I do this in render:

render() {
    const { DEFAULT_DAY_TIME} = json_constants;

and json_constants is imported like this:

import json_constants from '../../assets/json_constants';

It will not update when the text upon language change. I assume its because its already been imported and the language has already been set. So it will not re-import the file. I'm wondering if there is a way to change the language for those objects, or do I need to rethink the whole setup?

By the way, here is a simplified example of how those objects are used:

{DEFAULT_DAY_TIME.map(item => {
        return (
                <View key={item.value} style={styles.label} >
                        <Text>{item.name}</Text>
                </View>
        );
})}

Viewing all articles
Browse latest Browse all 29601

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>