0

I am trying to have a nested object with a structure similar to this one:

[EDIT] - Updated the object to better explain the situation

const theme = {
  primary: "#18f4c9",
  secondary: "#3264E8",
  background: "#1E3359",
  none: "rgba(0,0,0,0)",
  buttons: {
     borders: {
       primary: `none`,
       secondary: `1px solid ${theme.primary}`
     },
     background: {
       primary: `${theme.primary}`,
       secondary: `${theme.none}`
     },
     text: {
       primary: "#000",
       secondary: `${theme.primary}`
     }
  }
};

I get why I have this error now. I'm trying to use this in react for specific components. And when I init the object later I don't get the specifications I needed.

I get this error message:

Cannot access 'objectTest' before initialization

I don't know if this is even possible without hard coding it. But I have a much larger object but want to access these values several levels deep. Is this even possible?

  • 1
    No, not possible. The error explains why. The object doesn't exist before the initialization, so JS doesn't know what `objectTest.greeting` is. To avoid duplicate strings, just init the object, then do `objectText.valueToGet = \`${objectTest.greeting}\`;` in the next line. –  Oct 20 '19 at 16:59
  • @ChrisG I just updated the question with more information on the object I'm working. This is for a theme provider in React. I understand I have to init the object , but for some reason it doesnt get the data when I reproduce the component. – itstheandre Oct 20 '19 at 17:14
  • What do you mean by "reproduce the component"? Can you create a [mre]? –  Oct 20 '19 at 17:17
  • @ChrisG Does this work? BUTTON COMPONENT: const Button = styled.button` ${props => props.type && ` background-color: ${props => props.theme.test[props.type]} `} `; ` THEME const theme = { primary: 'hi', } theme.test = {1: `${theme.primary}. 2: null`} I created a theme like in the docs and nested everything inside it. So when I render the components its just like this: Dont know if its minimal enough, first time asking a question on StackOverflow. Thanks! – itstheandre Oct 20 '19 at 17:24

0 Answers0