How to store Configuration file and read it using React

前端 未结 4 754
执念已碎
执念已碎 2020-11-28 01:39

I am new on react.js I have implemented one component in which I am fetching the data from server and use it like,

CallEnterprise:function(TenantId){


             


        
相关标签:
4条回答
  • 2020-11-28 01:50

    You can use the dotenv package no matter what setup you use. It allows you to create a .env in your project root and specify your keys like so

    REACT_APP_SERVER_PORT=8000
    

    In your applications entry file your just call dotenv(); before accessing the keys like so

    process.env.REACT_APP_SERVER_PORT
    
    0 讨论(0)
  • 2020-11-28 01:53

    With webpack you can put env-specific config into the externals field in webpack.config.js

    externals: {
      'Config': JSON.stringify(process.env.NODE_ENV === 'production' ? {
        serverUrl: "https://myserver.com"
      } : {
        serverUrl: "http://localhost:8090"
      })
    }
    

    If you want to store the configs in a separate JSON file, that's possible too, you can require that file and assign to Config:

    externals: {
      'Config': JSON.stringify(process.env.NODE_ENV === 'production' ? require('./config.prod.json') : require('./config.dev.json'))
    }
    

    Then in your modules, you can use the config:

    var Config = require('Config')
    fetchData(Config.serverUrl + '/Enterprises/...')
    

    For React:

    import Config from 'Config';
    axios.get(this.app_url, {
            'headers': Config.headers
            }).then(...);
    

    Not sure if it covers your use case but it's been working pretty well for us.

    0 讨论(0)
  • 2020-11-28 02:03

    If you used Create React App, you can set an environment variable using a .env file. The documentation is here:

    https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables

    Basically do something like this in the .env file at the project root.

    REACT_APP_NOT_SECRET_CODE=abcdef
    

    You can access it from your component with

    process.env.REACT_APP_NOT_SECRET_CODE
    
    0 讨论(0)
  • 2020-11-28 02:05

    In case you have a .properties file or a .ini file

    Actually in case if you have any file that has key value pairs like this:

    someKey=someValue
    someOtherKey=someOtherValue
    

    You can import that into webpack by a npm module called properties-reader

    I found this really helpful since I'm integrating react with Java Spring framework where there is already an application.properties file. This helps me to keep all config together in one place.

    1. Import that from dependencies section in package.json

    "properties-reader": "0.0.16"

    1. Import this module into webpack.config.js on top

    const PropertiesReader = require('properties-reader');

    1. Read the properties file

    const appProperties = PropertiesReader('Path/to/your/properties.file')._properties;

    1. Import this constant as config

    externals: { 'Config': JSON.stringify(appProperties) }

    1. Use it as the same way as mentioned in the accepted answer

    var Config = require('Config') fetchData(Config.serverUrl + '/Enterprises/...')

    0 讨论(0)
提交回复
热议问题