Home > Software engineering >  How can I use React style imports for my own files?
How can I use React style imports for my own files?

Time:12-27

I would like to use this form often used in React or in this example react-redux

import redux, { useSelector, useDispatch } from 'react-redux';

So I tried the obvious:

I created an export file:

const exp = {};
exp.a = 'a-1';
exp.b = 'b-1'; 
export default exp;

And a file to import it:

import {a, b} from './40-export.js';  // this does not work
// import test from './40-export.js';  // this works
// const {a, b} = test;  // this works

CodePudding user response:

You need to both have an export default and named exports. Make standalone a and b variables that you export, and that you assign to properties of the default export object.

export const a = 'a-1';
export const b = 'b-1'; 
export const exp = { a, b }; // if you want this to be a named export too
export default exp;

CodePudding user response:

export const a = 2
export const b = 3
export default const c = 3

Then

import c , {a, b} from "your/file"

Note the use of {...} for named imports and the lack of it for default exports.

  • Related