[React] import vs require, js vs jsx extension

Import VS Require

You may notice that some example code has “require” and some has “import”. What’s the difference?

The difference is ES5 vs ES6.
ES5:

var React = require('react');
var ReactProp = React.PropTypes;

ES6:

import React, { Component, PropTypes } from 'react';

Then how about .js and .jsx extensions?

.js vs .jsx Extensions

As of now, the extensions don’t matter.

Before Babel, .js extension was only used when files used standard JavaScript only.

With Babel now, we process both js and jsx with same Babel config, so it makes more sense to use .js extension for both.

Some people still prefer to use .jsx extension for React files because it helps some IDEs to edit and give “better” colors. However, you can configure that on your own so this is not a problem.

Ref: https://github.com/facebookincubator/create-react-app/issues/87#issuecomment-234627904

Conclusion
Use import for ES6 and require for ES5.
Use .js extension.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s