How to add a link to a Toolbar item in React

04 Sep 20266 minutes to read

You can add a link inside the React Toolbar using a React template. Follow the guidelines below to add a link as a template in the React Toolbar.

  1. Declare a template within a function that returns a JSX element. The template can receive the item’s data object as an argument (e.g., props containing the React toolbar item); if the template does not need any arguments, there is no need to pass any properties.

  2. Assign the function as the value for the template option of the React toolbar item, for example <ItemDirective template={linkTemplate} />.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ToolbarComponent, ItemsDirective, ItemDirective } from '@syncfusion/ej2-react-navigations';
const App = () => {
    const AnchorTemplate = () => {
        return (<div>
        <a target="_blank" href="https://ej2.syncfusion.com/react/documentation/toolbar/getting-started/">
          Anchor Toolbar Link
        </a>
      </div>);
    }
    return (<div id='container'>
      <ToolbarComponent id="ej2Toolbar">
        <ItemsDirective>
          <ItemDirective text="Cut"/>
          <ItemDirective text="Copy"/>
          <ItemDirective type="Separator"/>
          <ItemDirective text="Paste"/>
          <ItemDirective type="Separator"/>
          <ItemDirective template={AnchorTemplate}/>
        </ItemsDirective>
      </ToolbarComponent>
    </div>);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<App />);
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ToolbarComponent, ItemsDirective, ItemDirective } from '@syncfusion/ej2-react-navigations';


const App = () => {
  const AnchorTemplate = (): JSX.Element => {
    return (
      <div>
        <a
          target="_blank"
          href="https://ej2.syncfusion.com/react/documentation/toolbar/getting-started/"
        >
          Anchor Toolbar Link
        </a>
      </div>
    );
  }
  return (
    <div id='container'>
      <ToolbarComponent id="ej2Toolbar">
        <ItemsDirective>
          <ItemDirective text="Cut" />
          <ItemDirective text="Copy" />
          <ItemDirective type="Separator" />
          <ItemDirective text="Paste" />
          <ItemDirective type="Separator" />
          <ItemDirective template={AnchorTemplate} />
        </ItemsDirective>
      </ToolbarComponent>
    </div>
  );
}

const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<App />);
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Toolbar Sample</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #container {
            max-width: 700px;
            min-width: 520px;
        }

        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='container'>
        <div id='element'>
            <div id='loader'>Loading</div>
        </div>
    </div>
</body>

</html>