abu abu
abu abu

Reputation: 7022

Conditional import in React.js

I need to import with condition. I am using below code and getting error.

if (window.location.hostname == 'sellbutton') {
  import serviceToUSe from '../public/service/sellbutton.com.json';
} else {
  import serviceToUSe from '../public/service/default.json';
}

Upvotes: 0

Views: 2456

Answers (1)

Karlo A. López
Karlo A. López

Reputation: 2668

I think you need to first declare an empty place to conditionally store the data after using require instead of importing:

//This might an object {} or array [] depending on your json data structure. 
var serviceToUSe = {};

if (window.location.hostname == 'sellbutton') {
  serviceToUSe = require('../public/service/sellbutton.com.json');
} else {
  serviceToUSe = require('../public/service/default.json');
}

Upvotes: 1

Related Questions