How to declare a global variable in React?

前端 未结 9 1246
自闭症患者
自闭症患者 2020-11-29 23:21

I initialized i18n translation object once in a component ( first component that loads in the app ) . That same object is required In all other components. I do

9条回答
  •  悲&欢浪女
    2020-11-30 00:21

    Create a file named "config.js" in ./src folder with this content:

    module.exports = global.config = {
        i18n: {
            welcome: {
                en: "Welcome",
                fa: "خوش آمدید"
            }
            // rest of your translation object
        }
        // other global config variables you wish
    };
    

    In your main file "index.js" put this line:

    import './config';
    

    Everywhere you need your object use this:

    global.config.i18n.welcome.en
    

提交回复
热议问题