Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js How to pass a variable from another js file that does not have export

I have an autogenerated fairly large js file that its content is just one big variable like this:

ConstDef.js:
var ConstantDefinitions = {
variable1: "blahbalhbalh",
variable2: "blahBlahBlah",
...
variable10000: "BlahbluuBleh"
}

How can I access the contents of this file in React.js? If I add export {ConstantDefinitions} manually at the bottom of ConstDef.js, I can easily write something like:

import { ConstantDefinitions } from './ConstDef';

const defHelper = {
  getStringByKey(key) {
    const def = ConstantDefinitions [key];
    return def;
  }
};

export default defHelper ;

My plan B is to write a script to somehow append export {ConstantDefinitions} to my autogenerated file, However, I am trying to access that object without manipulating the source.

like image 832
AleX_ Avatar asked Dec 05 '25 04:12

AleX_


1 Answers

You could generate a Json File instead of a JS Object, and import it from React Component

like image 173
Umberto Avatar answered Dec 07 '25 20:12

Umberto