React create constants file

后端 未结 4 1199
盖世英雄少女心
盖世英雄少女心 2021-01-31 07:08

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

ACTION_INVALID = \"This action is invalid!\"

and to use that in other components

4条回答
  •  野性不改
    2021-01-31 08:04

    Expanding on Monad's answer, for situations where you don't want to type myConstClass all the time:

    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 { ACTION_INVALID } from 'path/to/fileWithConstants';
    
    const errorMsg = ACTION_INVALID;
    

    (Also, if Monad's way works better for you, I believe the convention is for 'MyConstClass' to start with a capital letter, since it's acting like a class in code.)

提交回复
热议问题