javascriptreactjsconstants

React create constants file


How to create constants file like: key - value in ReactJs,

ACTION_INVALID = "This action is invalid!"

and to use that in other components

errorMsg = myConstClass.ACTION_INVALID;

Solution

  • I'm not entirely sure I got your question but if I did it should be quite simple:

    From my understanding you just want to create a file with constants and use it in another file.

    fileWithConstants.js:

    export const ACTION_INVALID = "This action is invalid!"
    export const CONSTANT_NUMBER_1 = 'hello I am a constant';
    export const CONSTANT_NUMBER_2 = 'hello I am also a constant';
    

    fileThatUsesConstants.js:

    import * as myConstClass from 'path/to/fileWithConstants';
    
    const errorMsg = myConstClass.ACTION_INVALID;
    

    If you are using react you should have either webpack or packager (for react-native) so you should have babel which can translate your use of export and import to older js.