0

I have an array of routes. Each route is it's own object which gets iterated to define. I would expect to be able to use the header key within main() using the 'this' keyword, but it's undefined. What am I missing here?

const loggedInRoutes = [
  {
    path: '/home',
    exact: true,
    isPrivate: true,
    header: 'Home',
    main() {
      console.log(this); // undefined
      return (
        <HeadWrap header={this.header}> // property 'header' undefined
          <Home />
        </HeadWrap>
      );
    },
  },
]
ram
  • 680
  • 5
  • 15
  • 4
    Does this answer your question? [Self-references in object literals / initializers](https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers) – gbalduzzi Aug 04 '20 at 14:07
  • I got it! Using the getter returned the returned object (go figure lol), but what I needed was a function! Thanks! – ram Aug 04 '20 at 14:45
  • I'm not sure why this works, but what I was trying originally does not. ``` let user = { name: 'John', age: 30, sayHi() { console.log(this.name); } }; user.sayHi(); // John ``` – ram Aug 04 '20 at 15:11

0 Answers0