I am new to the whole browserify thing.
I have been trying to use browserify + reactify + gulp to transform, minify and combine a React application.
As long as I have a single React.createClass
with a single module.exports = MyComponent
everything works fine.
Since I have several shared components I physically host in the same file and reuse across projects, I would like to export more than one component.
I have tried an array:
module.exports = [Component1, Component2]
and have also tried an object with multiple properties:
module.exports = {Comp1: Componenet1, Comp2: Component2}
and have also tried in-lining the calls to createClass
in the object, but that didn't help.
Is there a way to do this or do I have to split every component in to a seperate JSX file?
Use named exports to export multiple components in React, e.g. export function A() {} and export function B() {} . The exported components can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.
lazy(() => import('./OtherComponent')); This will automatically load the bundle containing the OtherComponent when this component is first rendered. React. lazy takes a function that must call a dynamic import() .
Run npm run build or npx react-scripts build to create an optimized production build for your React App and then run the command npx gulp to bundle all the JS and CSS files in the static build folder into the single main html file.
You can do like this, with an index.js file into your /components/
folder
import Users from './Users/Users';
import User from './Users/User';
module.exports = {
User,
Users
}
import { Users, User } from './components';
As you can see, I named my file index.js, it prevent me from write it in the import declaration. If you want to name your file with another name than index.js, you'd have to write the name of the file in the import, Node won't guess it ! ^^
I have put multiple components in one file and export the object like you suggested.
module.exports = {
comp1: Component1,
comp2: Component2
}
Then where they are used do
var comp1 = require('your/path/to/components').comp1;
var comp2 = require('your/path/to/components').comp2;
Define functions
function sum(a, b) {
return a + b
}
function sub(a, b) {
return a - b
}
function mul(a, b) {
return a * b
}
Define export
export { sum, sub, mul }
Import functions you need
import { sum, sub } from 'myfile'
or all the functions
import * as myfunctions from 'myfile'
and call as
sum(1+1)
or myfunctions.sum(1+1)
src: https://flaviocopes.com/how-to-export-multiple-functions-javascript/
I'm new to react and I faced this situation too. And this is how I overcame it.
Along to this question it also answers the following error:
export 'default' (imported as 'db') was not found in './index.js' (possible exports: auth, db).
1st method - use export default & don’t destructure while importing
index.js
const db = // module 1;
const auth = // module 2;
export default { db, auth}; // notice the default keyword here
while importing in other file
import db from "./index.js"
2nd method - use export & destructure while importing
index.js
const db = // module 1;
const auth = // module 2;
export { db, auth};
and while importing
import { db } from "./index.js"
Please note when to use destructuring while using the import statement
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