199

I initialized i18n translation object once in a component (a first component that loads in the app ). That same object is required In all other components. I don't want to re-initialize it in every component. What's the way around? Making it available to window scope doesn't help as I need to use it in the render() method.

Please suggest a generic solution for these problems and not i18n specific solution.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
sapy
  • 8,952
  • 7
  • 49
  • 60
  • 2
    You can use `Redux` or `Flux` library that can handle data or state globally. and you can pass it easily through all your components – vistajess Dec 18 '15 at 09:05
  • 2
    Any other way ? We started the project without any React Framework cause it was early day's of React . Now connecting every component to Redux store will require huge effort. – sapy Dec 18 '15 at 09:24
  • 4
    Would something like [react-global](https://github.com/captivationsoftware/react-global) work for you? – TwoStraws Dec 18 '15 at 09:28

14 Answers14

148

Beyond React

You might not be aware that an import is global already. If you export an object (singleton) it is then globally accessible as an import statement and it can also be modified globally.

If you want to initialize something globally but ensure its only modified once, you can use this singleton approach that initially has modifiable properties but then you can use Object.freeze after its first use to ensure its immutable in your init scenario.

const myInitObject = {}
export default myInitObject

then in your init method referencing it:

import myInitObject from './myInitObject'
myInitObject.someProp = 'i am about to get cold'
Object.freeze(myInitObject)

The myInitObject will still be global as it can be referenced anywhere as an import but will remain frozen and throw if anyone attempts to modify it.

Example of react state using singleton

https://codesandbox.io/s/adoring-architecture-ru3vt (see UserContext.tsx)

If using react-create-app

(what I was looking for actually) In this scenario you can also initialize global objects cleanly when referencing environment variables.

Creating a .env file at the root of your project with prefixed REACT_APP_ variables inside does quite nicely. You can reference within your JS and JSX process.env.REACT_APP_SOME_VAR as you need AND it's immutable by design.

This avoids having to set window.myVar = %REACT_APP_MY_VAR% in HTML.

See more useful details about this from Facebook directly:

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

King Friday
  • 25,132
  • 12
  • 90
  • 84
  • 12
    This is honestly a great tip, I was having trouble with my auth tokens since I'm using Server Side Rendering. I ended up loading from localstorage on the first load and then storing it into a separate Config file I can just import. Great answer. – lpetrucci May 02 '19 at 14:39
86

Why don't you try using Context?

You can declare a global context variable in any of the parent components and this variable will be accessible across the component tree by this.context.varname. You only have to specify childContextTypes and getChildContext in the parent component and thereafter you can use/modify this from any component by just specifying contextTypes in the child component.

However, please take a note of this as mentioned in docs:

Just as global variables are best avoided when writing clear code, you should avoid using context in most cases. In particular, think twice before using it to "save typing" and using it instead of passing explicit props.

Sudhir Dhumal
  • 902
  • 11
  • 22
Naisheel Verdhan
  • 4,905
  • 22
  • 34
  • 53
    close . but All component doesn't have parent child relationship , and Context does't work beyond that tree . I want i18n across the Application. – sapy Dec 18 '15 at 13:41
  • 2
    @sapy this is why you should use i18n as redux state not as a context variable see my answer here: http://stackoverflow.com/questions/33413880/react-redux-and-multilingual-internationalization-apps-architecture/43733280#43733280 – Fareed Alnamrouti May 08 '17 at 07:47
  • From my side I would recommend using redux instead, and specially for big Apps – Hosny Ben May 30 '20 at 18:40
  • 2
    Warning: answers like that might trigger the redux adepts. – Luca Fagioli Feb 28 '21 at 11:38
42

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
Alireza
  • 884
  • 9
  • 17
37

Is not recommended but.... you can use componentWillMount from your app class to add your global variables trough it... a bit like so:

componentWillMount: function () {
    window.MyVars = {
        ajax: require('../helpers/ajax.jsx'),
        utils: require('../helpers/utils.jsx')
    };
}

still consider this a hack... but it will get your job done

btw componentWillMount executes once before rendering, see more here: https://reactjs.org/docs/react-component.html#mounting-componentwillmount

Sudhir Dhumal
  • 902
  • 11
  • 22
rokyed
  • 420
  • 5
  • 9
  • 1
    It's a hack for certain use cases. I'm integrating a React app into the context of another non-React app at my company. This seems like a great way to expose public methods for the consuming app. Am I wrong? Is there a better way? – mccambridge Jan 16 '18 at 15:14
  • 2
    Also be aware that `componentWillMount` is being deprecated: https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html – RyanNerd May 29 '18 at 08:05
  • @mccambridge I know this is late but I would send a function from the non-React app into React that you would call to send the info. Btw, you can do this with iframes. – Nic Szerman Jul 23 '18 at 16:13
12

Here is a modern approach, using globalThis, we took for our React Native app.

globalThis is now included in...


appGlobals.ts

// define our parent property accessible via globalThis. Also apply the TypeScript type.
var app: globalAppVariables;

// define the child properties and their types. 
type globalAppVariables = {
  messageLimit: number;
  // more can go here. 
};

// set the values.
globalThis.app = {
  messageLimit: 10,
  // more can go here.
};


// Freeze so these can only be defined in this file.
Object.freeze(globalThis.app);


App.tsx (our main entry point file)

import './appGlobals'

// other code


anyWhereElseInTheApp.tsx

const chatGroupQuery = useQuery(GET_CHAT_GROUP_WITH_MESSAGES_BY_ID, {
  variables: {
    chatGroupId,
    currentUserId: me.id,
    messageLimit: globalThis.app.messageLimit, //  used here.
  },
});
GollyJer
  • 23,857
  • 16
  • 106
  • 174
9

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.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Shashank Bhong
  • 238
  • 3
  • 5
6

The best way I have found so far is to use React Context but to isolate it inside a high order provider component.

Sudhir Dhumal
  • 902
  • 11
  • 22
4
    Create a file :
    
    import React from "react";
    
    const AppContext = {};
    
    export default AppContext;
    
    then in App.js, update the value
    import AppContext from './AppContext';
    
    
    AppContext.username = uname.value;
    
    Now if you want the username to be used in another screen:
    
    import AppContext from './AppContext';
    
    AppContext.username to be used for accessing it.

Also found Another way :
Set the Value : localStorage.setItem('username', uname.value);

and to get the Value : localStorage.getItem("username");
Sandeep Jain
  • 1,019
  • 9
  • 13
2

Maybe it's using a sledge-hammer to crack a nut, but using environment variables (with Dotenv https://www.npmjs.com/package/dotenv) you can also provide values throughout your React app. And that without any overhead code where they are used.

I came here because I found that some of the variables defined in my env files where static throughout the different envs, so I searched for a way to move them out of the env files. But honestly I don't like any of the alternatives I found here. I don't want to set up and use a context everytime I need those values.

I am not experienced when it comes to environments, so please, if there is a downside to this approach, let me know.

Flip
  • 6,233
  • 7
  • 46
  • 75
2

For only declaring something, try this. Make sure MyObj is assigned at the proper time for you want to access it in render(), many ways was published before this thread. Maybe one of the simplest ways if undefined then create it does the job.

declare global {
  interface Window {
    MyObj: any;
  }
}
W.Perrin
  • 4,217
  • 32
  • 31
0

USE CUSTOM HOOKS

It is very simple if you use custom hooks

Refer this link https://stackoverflow.com/a/73678597/19969598

Full sample usage is available in the above post

0

This answer is for global part of question not I18N.

I wanted a global variable and function across all components of my application and without child-parent relationship.

This Answer is like a good one; but it was not completely clear to me so i had to test it my way.

I used below approach; not sure if this is a "good or bad practice" or even "off-topic"; but share in case help someone.

Global.jsx

const Global = () => { }

export default Global;

Global.var = 100;

Global.func = () => {
    Global.var += 1;
    alert(Global.var);
}

MyComponent1.jsx

import Global from "./Global";
import React from "react";

const MyComponent1 = () => {
    return ( <h1 onClick={Global.func}>COM1: {Global.var}</h1>)
}

export default MyComponent1;

MyComponent2.jsx

import Global from "./Global";
import React from "react";

const MyComponent2 = () => {
    return ( <h1 onClick={Global.func}>COM2: {Global.var}</h1>)
}

export default MyComponent2;

And anywhere like index.js

root.render(
  <div>
         .
         .
         .
          <MyComponent1/>
          <MyComponent1/>
          <MyComponent2/>
          <MyComponent2/>
         .
         .
         .
  </div>
);

Note: This way you have access to a global function or variable; but provided sample cannot update (render) screen itself cause no state or prop has been changed.

We can change the solution like this and keep ref of our components or DOM objects in our Global Zone like this (Not that i do not know its a good practice or even the worst case; so its on your own):

Global.jsx

const Global = () => { }

export default Global;

Global.var = 100;
Global.refs = [];
Global.inc = () => {
    Global.var += 1;
    Global.refs.forEach(ref => {
        ref.current.innerText = Global.var;
    });
}

MyComponent1.jsx, MyComponent2.jsx, ...

import Global from "./Global";
import React, { createRef } from "react";

const MyComponent1 = () => {
    const ref = createRef();
    Global.refs.push(ref);
    return (<div onClick={Global.inc}>
        <h2>COM1:</h2>
        <h3 ref={ref} >{Global.var}</h3>
    </div>);
};

export default MyComponent1;
Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36
0

I am a little late and maybe i have 0 votes but I think the best choice is to use the const count = useRef(0); Hook to create any object type you want and easy access it everywhere by calling count.current.

The useRef Hook allows you to persist values between renders.

It can be used to store a mutable value that does not cause a re-render when updated.

It can be used to access a DOM element directly.

This is an example where useRef is a number you can replace with whatever type or object you want:

import { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom/client";

function App() {
  const [inputValue, setInputValue] = useState("");
  const count = useRef(0);

  useEffect(() => {
    count.current = count.current + 1;
  });

  return (
    <>
      <input
        type="text"
        value={inputValue}
        onChange={(e) => setInputValue(e.target.value)}
      />
      <h1>Render Count: {count.current}</h1>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
Panagiotis Drakatos
  • 2,851
  • 4
  • 31
  • 43
-7

I don't know what they're trying to say with this "React Context" stuff - they're talking Greek, to me, but here's how I did it:

Carrying values between functions, on the same page

In your constructor, bind your setter:

this.setSomeVariable = this.setSomeVariable.bind(this);

Then declare a function just below your constructor:

setSomeVariable(propertyTextToAdd) {
    this.setState({
        myProperty: propertyTextToAdd
    });
}

When you want to set it, call this.setSomeVariable("some value");

(You might even be able to get away with this.state.myProperty = "some value";)

When you want to get it, call var myProp = this.state.myProperty;

Using alert(myProp); should give you some value .

Extra scaffolding method to carry values across pages/components

You can assign a model to this (technically this.stores), so you can then reference it with this.state:

import Reflux from 'reflux'
import Actions from '~/actions/actions'

class YourForm extends Reflux.Store
{
    constructor()
    {
        super();
        this.state = {
            someGlobalVariable: '',
        };
        this.listenables = Actions;
        this.baseState = {
            someGlobalVariable: '',
        };
    }

    onUpdateFields(name, value) {
        this.setState({
            [name]: value,
        });
    }

    onResetFields() {
        this.setState({
           someGlobalVariable: '',
        });
    }
}
const reqformdata = new YourForm

export default reqformdata

Save this to a folder called stores as yourForm.jsx.

Then you can do this in another page:

import React from 'react'
import Reflux from 'reflux'
import {Form} from 'reactstrap'
import YourForm from '~/stores/yourForm.jsx'

Reflux.defineReact(React)

class SomePage extends Reflux.Component {
    constructor(props) {
        super(props);
        this.state = {
            someLocalVariable: '',
        }
        this.stores = [
            YourForm,
        ]
    }
    render() {

        const myVar = this.state.someGlobalVariable;
        return (
            <Form>
                <div>{myVar}</div>
            </Form>
        )
    }
}
export default SomePage

If you had set this.state.someGlobalVariable in another component using a function like:

setSomeVariable(propertyTextToAdd) {
    this.setState({
       myGlobalVariable: propertyTextToAdd
   });
}

that you bind in the constructor with:

this.setSomeVariable = this.setSomeVariable.bind(this);

the value in propertyTextToAdd would be displayed in SomePage using the code shown above.

João Paulo
  • 6,300
  • 4
  • 51
  • 80
vapcguy
  • 7,097
  • 1
  • 56
  • 52
  • 4
    Sorry for being new here but I just learn React and not sure why this answer get so many downvoted – someoneuseless Feb 18 '20 at 07:58
  • 6
    @someoneuseless Exactly! And that's why I refuse to delete it and kowtow to the masses. Just because they want to use "Context" (whatever that is) and these other funny objects doesn't mean everyone needs to. High Five! – vapcguy Feb 19 '20 at 15:28