I came across a javascript library that uses the following syntax to import libraries:
import React, { Component, PropTypes } from 'react';
What is the difference between the above method and the following?
import React, Component, PropTypes from 'react';
This says:
This combines the two common syntaxes which you've probably seen
The first being used to import and name the default export, the second to import the specified named exports.
As a general rule, most modules will either provide a single, default export, or a list of named exports. It is somewhat less usual for a module to provide both a default export and named exports. However, in the case where there is one feature which is most commonly imported, but also additional sub-features, it is a valid design to export the first as the default, and the remaining ones as named exports. It is in such cases you would use the
import
syntax you refer to.The other answers are somewhere between wrong and confusing, possibly because the MDN documents at the time this question was asked were wrong and confusing. MDN showed the example
and said
name
is the "name of the object that will receive the imported values." But that's misleading and incorrect; first of all, there is only one import value, which will be "received" (why not just say "assigned to", or "used to refer to")name
, and the import value in this case is the default export from the module.Another way of explaining this is to note that the above import is precisely identical to
and the OP's example is precisely identical to
The MDN documentation went on to show the example
and claimed that it means
What MDN said here, and what other answers claim based on the incorrect MDN documentation, is absolutely wrong, and may be based on an earlier version of the spec. What this actually does is
(The default module export is the value exported with the
export default
syntax, which could also beexport {foo as default}
.)The MDN documentation writers may have gotten confused with the following form:
This imports all exports from
my-module
, and makes them accessible under names such asMyModule.name
. The default export is also accessible asMyModule.default
, since the default export is really nothing more than another named export with the namedefault
. In this syntax, there is no way to import only a subset of the named exports, although one could import the default export, if there is one, together with all the named exports, withThis will grab the exported
{ Component, PropTypes }
members from the'react'
module and assign them toComponent
andPropTypes
, respectively.React
will be equal to the module'sdefault
export.As noted by torazaburo below, this is the same as
which is shorthand for
Here's another example (link to gist):
I tested the second example with babel:
and got a syntax error.
For reference, you can read up on the new
import
documentation from MDN. However, it is apparently in need of technical review. Dr. Axel Rauschmayer's blog post is a better reference for now.