I have a side menu that successfully routes between pages. However, after routing the menu does not close automatically. I am assuming I can do this programmatically, but cannot figure out how to invoke the close() method on the IonMenu React component. Any ideas?
<IonMenu ref={menuRef} side='start' type='overlay' contentId='main-content'>
<IonItem button routerLink={page.path}>
<IonLabel>Home</IonLabel>
</IonItem>
</IonMenu>
import { menuController } from "@ionic/core";
<IonButton onClick={async () => await menuController.toggle()}>
Toggle Menu
</IonButton>
5 Likes
This doesn’t appear to work with @ionic/react@5.0.5
… Neither menuController.toggle()
nor menuController.close()
close the (unique) menu.
@JonathanMinson , have you succeeded to close the menu ?
I just tested this code and it does actually work… Can you be more specific in the use case where it doesn’t work?
Aaron’s solution worked for me. I’ve adapted to my use case:
const navigateToPage = (page: Page) => {
setActivePage(page.title);
setTimeout(() => menuController.toggle(), 250);
};
Stojko
August 10, 2020, 1:26pm
7
I would just add to @aaronksaunders soultion, if you have more then one menu, you can specify which one with menuController.toggle(“specific_menu_id”).
If the above didn’t work for you, check this out.
opened 12:44PM - 29 Dec 21 UTC
package: react
type: bug
### Prerequisites
- [X] I have read the [Contributing Guidelines](https://githu… b.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#creating-an-issue).
- [X] I agree to follow the [Code of Conduct](https://ionicframework.com/code-of-conduct).
- [X] I have searched for [existing issues](https://github.com/ionic-team/ionic-framework/issues) that already report this problem, without success.
### Ionic Framework Version
- [ ] v4.x
- [ ] v5.x
- [X] v6.x
### Current Behavior
When I try to programmatically toggle ion-menu using `await menuController.toggle(menuId)`, menu does not open.
### Expected Behavior
In previous versions of Ionic (5.9.3) calling toggle on menuController opened the menu correctly - see my other branch https://github.com/nika1001/ionic6-menu-controller-issue/tree/ionic5-working-menu
I would like to have the same behavior also in Ionic6.
### Steps to Reproduce
1. Start an application provided in the github: https://github.com/nika1001/ionic6-menu-controller-issue
2. Pressing buttons under "Toggle menu by MenuController" header does nothing
### Code Reproduction URL
https://github.com/nika1001/ionic6-menu-controller-issue
### Ionic Info
Ionic:
Ionic CLI : 6.18.1 (C:\Tools\nvm\v16.10.0\node_modules\@ionic\cli)
Ionic Framework : @ionic/react 6.0.1
Capacitor:
Capacitor CLI : 3.3.3
@capacitor/android : not installed
@capacitor/core : 3.3.3
@capacitor/ios : not installed
Utility:
cordova-res : not installed globally
native-run : 1.5.0
System:
NodeJS : v16.10.0 (C:\Program Files\nodejs\node.exe)
npm : 8.0.0
OS : Windows 10
### Additional Information
Reason for using menuController is that I need multiple menus on the same side and in order to do that I had to first enable a menu to be toggled:
```
const toggleMenu = async (menuId: string) => {
await menuController.enable(true, menuId)
await menuController.toggle(menuId)
}
```
Inspired by: https://forum.ionicframework.com/t/allow-multiple-menus/189934/4
If I don't enable the menu, I can toggle only 1 of the menus assigned to "start" side. This is demonstrated by buttons under "Toggle menu directly" header in my demo application: https://github.com/nika1001/ionic6-menu-controller-issue
1 Like
Late to the party, but seems one can invoke the menu method(s) on the ref
instance:
const menuRef = React.useRef<HTMLIonMenuElement>(null);
<IonMenu ref={menuRef} contentId="main-content">
...
</IonMenu>
then call it wherever: menuRef.current?.close()
or menuRef.current?.toggle()
3 Likes
This was actually really helpful, thank you