user17331023
user17331023

Reputation: 51

How can I use React style imports for my own files?

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

Upvotes: 0

Views: 34

Answers (2)

krirkrirk
krirkrirk

Reputation: 437

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.

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370809

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;

Upvotes: 2

Related Questions