Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
How to deconstruct properites on import based on theme?

I have a colors.ts file:

export const black = '#0C0C0C';
export const blue = '#22618E';

and when ever I want to import a specific color I import it like:

import {black} from 'Shared';

In my Shared folder I have an index.ts that exports the colors:

export * from './colors';

Now I want to be able to export and Import the same way when using a theme.

Example

colors.ts:

const DefaultTheme = {
  black: '#0C0C0C',
  blue: '#22618E',

}

const SpecialTheme= {
  black: 'red',
  blue: 'yellow',

}

export const ColorsBytheme = () => {
 const context = myContext();

 return context.Default? DefaultTheme :SpecialTheme;
}

Problem:

I can not deconstruct/import the properties directly, this gives me undefined:

import {black} from 'Shared';

How can I accomplish this?

EDIT:

I have also tried by using a self invoking function when exporting:

const ColorsBytheme = () => {
  return DefaultTheme;
};

export default ColorsBytheme();

When I import

import { black } from 'Shared';

It is still undefined

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Based on your code example, you are currently exporting ColorsBytheme as a function that generates/returns the theme object...therefore, your import/deconstruction would need to look something along the lines of:

import { ColorsBytheme } from "Shared"
const { black } = ColorsBytheme();

Another option if you wish to have a single line would be to use require instead of import:

const { black } = require("Shared").ColorsBytheme()

Variables that are imported are just that, static variables. Functions are not evaluated when they are imported, therefore, in order for the values to be dynamic, the function needs to be executed.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!