Detecting production vs. development React at runtime

后端 未结 3 396
清酒与你
清酒与你 2021-01-30 12:00

Is it possible to detect whether the current version of React is development or production at runtime? I\'d like to do something like this:

if (React.isDevelopme         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-30 12:37

    I use a helper file (in Typescript):

    import process from "process";
    
    const development: boolean = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
    
    export default function isDev(): boolean
    {
        return development;
    }
    

    Then elsewhere I use it like this:

    import isDev from "./helpers/DevDetect";
    
    if (isDev())
    {
        ...
    }
    

提交回复
热议问题