'I have trouble looking for a solution on where and how to create property file in React JS

Hi I just started creating project in react js can we create property file outside of the react app and call those values in react app. If yes than where I need to create it and what should be the extension of the file.



Solution 1:[1]

Solution 1 :

Here I would suggest you to create .env file with following steps

  1. create a file called .env in the root of your project
  2. REACT_APP_NOT_SECRET_CODE=abcdef
  3. To access environment variables you can write process.env.REACT_APP_NOT_SECRET_CODE in react code

You must create custom environment variables beginning with REACT_APP_

Here is the link you can read more

Solution 2

.env file will not allow to change value at run time. To change value at run time you try following solution

Config.js

    /**
     * Init GLOBAL_VARIABLES to store global config
     */
    if (typeof window.global === 'undefined') {
        window.global = {config: {}};
    }
    const GLOBAL_VARIABLES = window.global.config;

    export default GLOBAL_VARIABLES;

Then when you want to store value from anywhere your React component you can import GLOBAL_VARIABLES and assign the value

    import GLOBAL_VARIABLES from "./Config";
    .....
    GLOBAL_VARIABLES.session = session;
    GLOBAL_VARIABLES.countries = countries;
    ...

Similarly you can also fetch the value where ever you want back

    console.log(GLOBAL_VARIABLES.countries)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1