I am not sure why you are not getting the menu items as they are passed value and is defined in the base class. Ideally, you should get it.
Also, what you are trying can be achieved without any renderer code and regardless of the approach you take, you will need to make sure that you reinstate the state back once the navigation finishes.
Step 1: Add an attribute for storing either selected menu items' label or ID. I have used the ID.
<aura:attribute name="currentSelectedId" type="String"/>
Step 2: Create two iterations:
<aura:if isTrue="{!item.id == v.currentSelectedId}">
<aura:iteration items="{!item.menuItems}" var="subItem">
<a class="slds-m-left--large" data-menu-item-id="{!item.id}"
onclick="{! c.onClick }">{!item.label}</a>
</aura:iteration>
</aura:if>
<aura:if isFalse="{!item.id == v.currentSelectedId}">
<aura:iteration items="{!v.menuItems}" var="item">
<a class="slds-m-left--large" data-menu-item-id="{!item.id}"
onclick="{! c.onClick }">{!item.label}</a>
</aura:iteration>
</aura:if>
Step 3: Add on click handler
onClick: function (component, event) {
event.preventDefault();
let id = event.target.dataset.menuItemId;
if (id) {
component.set('v.currentSelectedId', id);
component.getSuper().navigate(id);
**// Store current Id somewhere in storage or cookie and read it on load.**
}
},
forceCommunity:navigationMenuBase
correct? My guess as to whymenuItems
is null on init is that they may not be available until after initialization. If that is the case I would use a change handler to grab the values in the JS controller.