Detecting production vs. development React at runtime

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

    I wanted access to this from the index.html and desired a solution which didn't involve ejecting webpack or configuring it with additional modules and I came up with this.

    Sources are David's answer above and the create-react-app documentation for using environment variables in the html file

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

提交回复
热议问题