How to add or remove items in Context Menu in React
04 Sep 20268 minutes to read
The React Context Menu component provides dynamic item management capabilities, allowing you to add or remove menu items programmatically. This functionality enables dynamic menu systems that adapt to changing application states, user permissions, or contextual requirements.
React Context Menu items can be dynamically modified using the insertAfter, insertBefore, and removeItems public methods. These methods are exposed on the ContextMenuComponent instance and are accessed through a component ref.
| Method | Signature | Returns | Description |
|---|---|---|---|
insertAfter |
insertAfter(items: MenuItemModel[], target: string) |
void |
Inserts the supplied items after the menu item whose text matches the target argument. |
insertBefore |
insertBefore(items: MenuItemModel[], target: string) |
void |
Inserts the supplied items before the menu item whose text matches the target argument. |
removeItems |
removeItems(items: string[]) |
void |
Removes the menu items whose text values match the supplied array. |
The
targetargument forinsertAfter/insertBeforematches an existing menu item by itstextvalue. Mutation of submenu (nested) items via these methods is not supported; use item binding / state updates to restructure nested menus.
Calling the methods safely
Call these methods after the component has initialized — typically inside the created event handler — and access the instance through a React ref. Calling them before created raises a “no item found” error because the menu structure has not been built yet.
In the following example, the Display Settings menu items are added before the Personalize item, the Sort By menu items are added after the Refresh item, and the Paste item is removed from the React Context Menu.
import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
function App() {
let cMenu;
let menuItems = [
{
items: [
{
text: 'Large icons'
},
{
text: 'Medium icons'
},
{
text: 'Small icons'
}
],
text: 'View'
},
{
text: 'Refresh'
},
{
text: 'Paste'
},
{
separator: true
},
{
text: 'New'
},
{
separator: true
},
{
text: 'Personalize'
}
];
function created() {
cMenu.insertAfter([{ text: 'Sort By' }], 'Refresh');
cMenu.insertBefore([{ text: 'Display Settings' }], 'Personalize');
cMenu.removeItems(['Paste']);
}
;
return (<div className="container">
<div id='target'>Right click / Touch hold to open the ContextMenu</div>
<ContextMenuComponent id="cmenu" ref={(scope) => cMenu = scope} target='#target' items={menuItems} created={created}/>
</div>);
}
export default App;
ReactDom.render(<App />, document.getElementById('element'));import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
function App() {
let cMenu: ContextMenuComponent;
let menuItems: MenuItemModel[] = [
{
items: [
{
text: 'Large icons'
},
{
text: 'Medium icons'
},
{
text: 'Small icons'
}
],
text: 'View'
},
{
text: 'Refresh'
},
{
text: 'Paste'
},
{
separator: true
},
{
text: 'New'
},
{
separator: true
},
{
text: 'Personalize'
}];
function created(): void {
cMenu.insertAfter([{text: 'Sort By'}] , 'Refresh');
cMenu.insertBefore([{text: 'Display Settings'}] , 'Personalize');
cMenu.removeItems(['Paste']);
};
return (
<div className="container">
<div id='target'>Right click / Touch hold to open the ContextMenu</div>
<ContextMenuComponent id="cmenu" ref={(scope) => cMenu = scope as ContextMenuComponent} target='#target' items={menuItems} created={created}/>
</div>
);
}
export default App;
ReactDom.render(<App />,document.getElementById('element'));