Add or remove context menu items in React Context menu component

7 Oct 20257 minutes to read

The ContextMenu component provides dynamic item management capabilities, allowing you to add or remove menu items programmatically at runtime. This functionality enables responsive menu systems that adapt to changing application states, user permissions, or contextual requirements.

ContextMenu items can be dynamically modified using the insertAfter, insertBefore and removeItems methods.

The insertAfter method adds new menu items after a specified target item, while insertBefore adds items before the target. The removeItems method removes specified items from the menu structure.

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, and the Paste item is removed from 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'));