tristantzara
tristantzara

Reputation: 5897

ES6 export object properties separately

Say, I have an object like {foo: 5, bar: 10} and I wanna export foo and bar from it separately, then when I do import {foo} from './path/to/file';

I could get foo equal to 5, as if I did export const foo = 5; export const bar = 10;

How can I do it?

Upvotes: 22

Views: 13606

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161447

Exported values need their own top-level variable name. The easiest option might be something like this:

const obj = {foo: 5, bar: 10};

export const {foo, bar} = obj;

Really though, if the object is already declared in the file, you may be better off declaring the exports with the values directly.

Upvotes: 44

Related Questions