title: this.state is undefined inside function - React JS
body: <p>Why do i get state is undefined here? I tried various solutions but none worked for me. Can someone point out what am doing wrong here</p>
  
  <p>Edit:</p>
  
  <p>Complete Code of the component</p>
  
  <pre><code>  class Sidebar extends React.Component {
      constructor (props) {
        super(props)
        this.state = {
          menuSelected: menuOptions.menuItems[0].title,
        }
        this.createMenuListItem = this.createMenuListItem.bind(this)
      }
      render () {
        console.log(this.state.menuSelected)
        function getChildItems (child) {
          let childItems = child.children.map(function (menuItem, index) {
            return createMenuListItem(menuItem)
          })
          return (
            &lt;li key={child.title} className='dropdown'&gt;
              &lt;i className={child.iconName} /&gt;
              &lt;span className='dropdown-toggle'&gt;{child.title}&lt;/span&gt;
              &lt;ul className=''&gt;
                {childItems}
              &lt;/ul&gt;
            &lt;/li&gt;
          )
        }
        function createMenuListItem (menuItem) {
          if (menuItem.hasChild === 'N') {
            console.log(this.state.menuSelected)
            return (
              &lt;li key={menuItem.title}&gt;
                &lt;a href='#'&gt;
                  &lt;i className={menuItem.iconName} /&gt;
                  &lt;span&gt;{menuItem.title}&lt;/span&gt;
                &lt;/a&gt;
              &lt;/li&gt;
            )
          } else {
            return getChildItems(menuItem)
          }
        }
  
        let menuItemsLoaded = menuOptions.menuItems.map((menuItem, index) =&gt; {
          return createMenuListItem(menuItem)
        })
        return (
          &lt;nav id='sidebar'&gt;
            &lt;ul className='list-unstyled'&gt;
              {menuItemsLoaded}
            &lt;/ul&gt;
          &lt;/nav&gt;
        )
      }
    }
  
    export default Sidebar
  </code></pre>
  
  <p>Also can somebody possibly suggest a better option when the sidebar content is loaded dynamically from the server</p>