1

I want to import two objects from different files based on some conditions, and export the imported object with different name:

if(condition1)
    import {obj3, obj4} from './file1';
else
    import {obj5, obj6} from './file2';

export {obj1, obj2}
3
  • you can try the factory pattern for this. Commented Aug 13, 2018 at 11:24
  • 1
    concwerning the rename, you can use the as syntax Commented Aug 13, 2018 at 11:30
  • In a project, I wanted to free my app from Sentry's logging capability while it was in development mode. So I did this and it works on my App.js. export default __DEV__ ? App : Sentry.wrap(App) Commented Aug 10, 2022 at 8:20

1 Answer 1

5

You could have an intermediate importer/exporter that re-export a given import based on a condition, like this:

//exportSelector.js;
import {obj1, obj2}
let exportedObj = condition1 ? obj1 : obj2;
export exportedObj;

//import.js
import exportedObj;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.