Skip to content

How to declare a global variable in React?

If you need to declare a global variable in React, then you can do the following:

Create a file called config.js

module.exports = global.config = { something: { somethingelse: { amount: 123, name: "Andrew" } } };
Code language: JavaScript (javascript)

Then import the config file where you need it:

import './config';
Code language: JavaScript (javascript)

Then where you need it in your code:

global.config.something.somethingelse.amount
Code language: JavaScript (javascript)

or

global.config.something.somethingelse.name
Code language: JavaScript (javascript)

See also  Calculate the biggest of 3 numbers using Java
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x