Detecting production vs. development React at runtime

后端 未结 3 419
清酒与你
清酒与你 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:57

    This is best done emulating the Node way of doing things with your build tool - webpack, browserify - by exposing process.env.NODE_ENV. Typically, you'll have it set to "production" in prod and "development" (or undefined) in dev.

    So your code becomes:

    if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
        // dev code
    } else {
        // production code
    }
    

    For how to set it up, see envify or Passing environment-dependent variables in webpack

提交回复
热议问题