I'm writing a Mixin for ReactJS. I'd like it to do some validation, but only when ReactJS is being used in development mode.
How can I determine from JavaScript whether ReactJS is in development or production mode?
No, React isn’t better than JavaScript. But comparing the two is tricky because React is a JavaScript library – it takes the existing code for JavaScript and makes it more powerful in a specific way.
process.env.NODE_ENV != development is for production build In React Native, global environment variable DEV is available to use. It is a javascript environment variable which we can use to set to true for development and false for production. This is why we can check to react native is using development or production build.
ReactJS is a frontend Javascript framework, which means it’s Javascript code you’ll find in the <script> elements of the page. So just check the source of the website you want to check if it has ReactJS.
Let’s see now how to create react app in production mode. Step 1: Open terminal and run the following command to create project folder of the react application: Step 2: Move into the project folder: Project Structure: The initial project structure will look like the following.
The ReactJS source uses a variable called __DEV__
to track this, but it's not exported, so it's unavailable to your Mixin.
Its consequences are, however. When you break an invariant, for example, dev mode ReactJS will give you a nice description of what went wrong. In production mode, it will give a generic error telling you to use the dev version.
We can use this to build a function which determines if React is in dev mode:
function isDevReact() {
try {
React.createClass({});
} catch(e) {
if (e.message.indexOf('render') >= 0) {
return true; // A nice, specific error message
} else {
return false; // A generic error message
}
}
return false; // should never happen, but play it safe.
};
This works because the exception for not implementing a render
method is different in the two modes:
Development: "Invariant Violation: createClass(...): Class specification must implement a `render` method. Inline JSX script:16"
Production: "Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings. Inline JSX script:16"
The word "render" is specific to the invariant we violated, so it only shows up in the dev version's exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With