Faisal
Faisal

Reputation: 604

Get Object by condition in reactjs

i have a file jsx like

const Routes = {
    item: '/Dev/Item',
    count: '/Dev/Count',
};

module.exports = Routes;

i want the object in condition wise like

const Routes = {};
    if(true){
        this.Routes = {
            item: '/Dev/Item',
            count: '/Dev/Count',
        };
    }
    else{
      this.Routes = {
            item: '/Prod/Item',
            count: '/Prod/Count',
        };
    }

    module.exports = Routes;

i can import this file at any component and can access Route.item or Route.Count how can i do this with if Else?

Upvotes: 1

Views: 375

Answers (1)

Tholle
Tholle

Reputation: 112867

const is block scoped, so Routes will not be defined after your if statement.

You could instead create a variable with let before the if statement and assign a value to that.

let Routes;

if (true) {
  Routes = {
    item: "/Dev/Item",
    count: "/Dev/Count"
  };
} else {
  Routes = {
    item: "/Prod/Item",
    count: "/Prod/Count"
  };
}

module.exports = Routes;

Upvotes: 2

Related Questions