How to enable or disable Context Menu items in React

04 Sep 20267 minutes to read

The React Context Menu component supports dynamic state management of menu items through the enableItems method. This functionality allows you to control menu item availability based on application state, user permissions, or contextual conditions, offering only relevant actions. Disabled items remain in the menu but are visually dimmed and not interactive.

The enableItems method accepts three parameters: an array of item identifiers (items), a boolean value (enable) to specify the desired state, and an optional isUniqueId parameter to indicate whether the identifiers refer to unique IDs or text content. When enable is set to true, the specified items become interactive; when false, they become disabled and visually dimmed.

In the following example, the Display Settings parent item and Medium icons sub menu item are disabled to demonstrate different levels of menu item control.

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: 'Sort By'
        },
        {
            text: 'Refresh'
        },
        {
            separator: true
        },
        {
            text: 'New'
        },
        {
            separator: true
        },
        {
            text: 'Display Settings'
        },
        {
            text: 'Personalize'
        }
    ];
    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} beforeOpen={beforeOpen}/>
        </div>);
    function created() {
        cMenu.enableItems(['Display Settings'], false);
    }
    ;
    function beforeOpen() {
        cMenu.enableItems(['Medium icons'], false);
    }
    ;
}
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: 'Sort By'
        },
        {
            text: 'Refresh'
        },
        {
            separator: true
        },
        {
            text: 'New'
        },
        {
            separator: true
        },
        {
            text: 'Display Settings'
        },
        {
            text: 'Personalize'
        }
    ];

    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} beforeOpen={beforeOpen} />
        </div>
    );
    function created() {
        cMenu.enableItems(['Display Settings'], false);
    };
    function beforeOpen() {
        cMenu.enableItems(['Medium icons'], false);
    };
}
export default App;
ReactDom.render(<App />,document.getElementById('element'));