How to declare a global variable in React?

前端 未结 9 1230
自闭症患者
自闭症患者 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-29 23:58

    Can keep global variables in webpack i.e. in webpack.config.js

    externals: {
      'config': JSON.stringify(GLOBAL_VARIABLE: "global var value")
    }
    

    In js module can read like

    var config = require('config')
    var GLOBAL_VARIABLE = config.GLOBAL_VARIABLE
    

    Hope this will help.

提交回复
热议问题