What is the best way to import some modules in all of the files of the project, so I don't have to write stuff like:
import React from 'react';
import Reflux from 'reflux';
import reactMixin from 'react-mixin';
in almost every single file?
Safari, Chrome, Firefox and Edge all support the ES6 Modules import syntax. Here's what they look like. Simply add type="module" to your script tags and the browser will load them as ES Modules. The browser will follow all import paths, downloading and executing each module only once.
ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.
One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with . js extension as opposed to .
The other answer covers this, but not with valid ES6, so I'm adding my own.
Make a central file to import your react components, in some central react.js
file
export {default as React} from 'react';
export {default as Reflux} from 'reflux';
export {default as reactMixin} from 'react-mixin';
Then in the files where you need to use these three, you could do
import {React, Reflux, reactMixin} from './react';
to import all three into your component file.
Create a "base" that declares common imports, then you can import that one file.
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