React Menu - items
The items array can contain:
- Objects with fields described in this section
- Objects with any other fields. In this case, specify the itemTemplate.
If you need to update the UI component items, reassign the entire items array as shown in the following example:
- menuInstance.option('items', newItems);
As an alternative to items, you can use the dataSource property. It accepts the DataSource object, whose underlying stores provide an API that allows you to update individual items without reassigning the entire item collection.
component
An alias for the template property specified in React. Accepts a custom component. Refer to Using a Custom Component for more information.
icon
This property accepts one of the following:
- The icon's URL
- The icon's name if the icon is from the DevExtreme icon library
- The icon's CSS class if the icon is from an external icon library (see External Icon Libraries)
- The icon in the Base64 format
- The icon in the SVG format. Ensure that the source is reliable.
linkAttr
Specifies link attributes for the url option.
You can assign all attributes described in the Attributes article except for the following:
href
Use the url property to specify a link instead.Global attributes
You can specify onlyid
,class
, and aria-* global attributes.
If you assign a
target: '_blank'
attribute, add arel: 'noopener'
orrel: 'noreferrer'
attribute to avoid security issues.When an item receives focus, screen readers should announce a description of the link. Add an
aria-label
attribute with the description to inform users.
The following code opens a link in a new window or tab:
- import React from 'react';
- import Menu, { Item } from 'devextreme-react/menu';
- const linkAttr = {
- target: '_blank',
- rel: 'noopener'
- };
- function App() {
- return (
- <Menu>
- <Item
- text="Home"
- url="https://js.devexpress.com"
- linkAttr={linkAttr}
- />
- </Menu>
- );
- }
- export default App;
render
An alias for the template property specified in React. Accepts a rendering function. Refer to Using a Rendering Function for more information.
template
The following types of the specified value are available.
- Assign a string containing the name of the required template.
- Assign a DOM Node of the template's container.
The following example adds a custom item to the component. In React, specify the render or component properties.
- import React from 'react';
- import Menu, {
- Item
- } from 'devextreme-react/menu';
- const renderCustomItem = () => {
- return <div>Custom Item</div>;
- }
- const App() = () => {
- return (
- <Menu ... >
- <Item ...
- render={renderCustomItem}
- >
- </Item>
- </Menu>
- );
- }
- export default App;
See Also
url
The following code opens a link in the same frame the item was clicked:
- import React from 'react';
- import Menu, { Item } from 'devextreme-react/menu';
- function App() {
- return (
- <Menu>
- <Item
- icon="home"
- url="https://js.devexpress.com"
- />
- </Menu>
- );
- }
- export default App;
To assign link attributes, specify the linkAttr property.
If you specify a rendering function or a component for an item, do not specify this property. Define a link in the markup instead.
- import React from 'react';
- import Menu, { Item } from 'devextreme-react/menu';
- const renderLink = () => {
- return <a href="http://js.devexpress.com" target="_blank">Home</a>;
- }
- function App() {
- return (
- <Menu>
- <Item render={renderLink} />
- </Menu>
- );
- }
- export default App;
If you use routing for your project, specify <Link>
links inside items. Alternatively, you can implement the onItemClick event handler to process clicks on links.
- import React from 'react';
- import Menu, { Item } from 'devextreme-react/menu';
- import { Routes, Outlet, Route, Link } from "react-router-dom";
- function App() {
- return (
- <Menu>
- <Item>
- <Link to="/">Home</Link>
- </Item>
- <Item>
- <Link to="/about">About</Link>
- </Item>
- </Menu>
- <Routes>
- <Route exact path="/" element={<Home />}></Route>
- <Route path="/about" element={<About />}></Route>
- </Routes>
- <Outlet />
- );
- }
- export default App;
If you have technical questions, please create a support ticket in the DevExpress Support Center.