Skip to main content

useContextMenu

The hook returns 2 methods:

  • show
  • hideAll
ParamRequiredTypeDescripton
idnostring | numberUnique id to identify the menu. Can be overriden on each call
propsnoanyPassed to the Item onClick callback. Can be overriden on each call
import { useContextMenu } from 'react-contexify';

const { show, hideAll } = useContextMenu({
id: "menuId",
props: {
key: "value"
}
});

show

ParamRequiredTypeDescripton
eventDOM eventThe event used to display the menu
idnostring | numberUnique id to identify the menu. Can be overriden on each call
propsnoanyPassed to the Item onClick callback. Override props defined during initialization if any
positionno{ x: number, y: number }Use a custom position for the Menu
import { useContextMenu } from 'react-contexify';

const { show, hideAll } = useContextMenu({
id: "menuId",
props: {
key: "value"
}
});

// basic usage
function displayMenu(e: React.MouseEvent) {
show({
event: e
})
}

// multiple menu handling
function displayMenu(e: React.MouseEvent) {
show({
event: e,
id: "another-menu-id"
})
}

// custom position
function displayMenu(e: React.MouseEvent) {
show({
event: e,
position: {
x: 100,
y: 200
}
})
}

// pass props
function displayMenu(e: React.MouseEvent) {
show({
event: e,
props: {
key: "value1",
foo: false
}
})
}

hideAll

import { useContextMenu } from 'react-contexify';

const { show, hideAll } = useContextMenu({
id: "menuId"
});

// hide all menus
hideAll()