Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i call static variables from outside of my Component?

Tags:

reactjs

I want to create my static variables outside of my component and then call and using it in my component. Btw i dont want outside variables as a component or props.

like image 484
Arty Avatar asked Oct 27 '25 00:10

Arty


2 Answers

You can create class like that

export default class TableConst {
  static LocaleText = {
    values: {
      to: "-",
      page: " ",
    }
  };
 }

Then import that static class and use it in your main page

 import TableConst from "../TableConst";
 ...
 ...
 ...
 let columnDefs={TableConst.LocaleText.values}
like image 134
eyeniay Avatar answered Oct 28 '25 15:10

eyeniay


If you define variable inside the class using a static keyword you can access those variable using class Name directly without instantiating it.

class CustomVariables {
   static MyVariable = 'SomeContent';
   static MyVariable2 = 'SomeContent2';
}

export default CustomVariables;

Also you could write those variables within the class and return an instance of the class

class CustomVariables {
   constructor() {
       this.MyVariable = 'SomeContent';
       this.MyVariable2 = 'SomeContent2';
   }
}

export default CustomVariables();

and you can use it like below for both the caes

import CustomVariables from 'path/to/CustomVariables';

console.log(CustomVariables.MyVariable)
like image 41
Shubham Khatri Avatar answered Oct 28 '25 16:10

Shubham Khatri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!