Getting Started

8 May 202513 minutes to read

This section explains how to create a simple MultiSelect component and configure its available functionalities in React.

To get start quickly with React MultiSelect component, you can check on this video:

Dependencies

The following list of dependencies are required to use the MultiSelect component in your application.

|-- @syncfusion/ej2-react-dropdowns
|-- @syncfusion/ej2-react-base
|-- @syncfusion/ej2-dropdowns
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-lists
    |-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-navigations
    |-- @syncfusion/ej2-popups
        |-- @syncfusion/ej2-buttons

Installation and configuration

To easily set up a React application, use create-vite-app, which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.

Note: To create a React application using create-react-app, refer to this documentation for more details.

To create a new React application, run the following command.

npm create vite@latest my-app

To set-up a React application in TypeScript environment, run the following command.

npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev

To set-up a React application in JavaScript environment, run the following command.

npm create vite@latest my-app -- --template react
cd my-app
npm run dev

Adding syncfusion® packages

All the available Essential® JS 2 packages are published in npmjs.com public registry. You can choose the component that you want to install.

To install MultiSelect component, use the following command

npm install @syncfusion/ej2-react-dropdowns --save

Adding MultiSelect component

Now, you can start adding MultiSelect component in the application. For getting started, add the MultiSelect component in src/App.tsx file using following code.

Add the below code in the src/App.tsx to initialize the MultiSelect.

[Class-component]

import { MultiSelectComponent  } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component<{}, {}> {
  public render() {
    return (
       // specifies the tag for render the MultiSelect component
      <MultiSelectComponent  id='mtselement'/>
    );
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));

[Functional-component]

import { MultiSelectComponent  } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App(){
    return (
       // specifies the tag for render the MultiSelect component
      <MultiSelectComponent  id='mtselement'/>
    );
}
ReactDOM.render(<App />, document.getElementById('sample'));

Adding CSS reference

Import the MultiSelect component required CSS references as follows in src/App.css.

/* import the MultiSelect dependency styles */

@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-react-dropdowns/styles/material.css";

Binding data source

After initialization, populate the data using dataSource  property. Here, an array of string values is passed to the MultiSelect component.

[Class-component]

import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {
  // define the array of data
  private sportsData: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
  public render() {
    return (
        // specifies the tag for render the MultiSelect component
      <MultiSelectComponent id="mtselement" dataSource={this.sportsData} />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));

[Functional-component]

import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App(){
  // define the array of data
  let sportsData: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
    return (
        // specifies the tag for render the MultiSelect component
      <MultiSelectComponent id="mtselement" dataSource={sportsData} />
    );
}
ReactDOM.render(<App />, document.getElementById('sample'));

Run the application

Now run the npm run dev command in the console to start the development server. This command compiles your code and serves the application locally, opening it in the browser.

npm run dev

[Class-component]

import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    // define the array of data
    sportsData = ['Badminton', 'Cricket', 'Football', 'Golf', 'Tennis'];
    render() {
        return (
        // specifies the tag for render the MultiSelect component
        <MultiSelectComponent id="mtselement" dataSource={this.sportsData} placeholder="Find a game"/>);
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {
  // define the array of data
  private sportsData: string[] = ['Badminton', 'Cricket', 'Football', 'Golf', 'Tennis'];
  public render() {
    return (
        // specifies the tag for render the MultiSelect component
      <MultiSelectComponent id="mtselement" dataSource={this.sportsData}  placeholder="Find a game" />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));

[Functional-component]

import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
    // define the array of data
    let sportsData = ['Badminton', 'Cricket', 'Football', 'Golf', 'Tennis'];
    return (
    // specifies the tag for render the MultiSelect component
    <MultiSelectComponent id="mtselement" dataSource={sportsData} placeholder="Find a game"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
  // define the array of data
  let sportsData: string[] = ['Badminton', 'Cricket', 'Football', 'Golf', 'Tennis'];
    return (
        // specifies the tag for render the MultiSelect component
      <MultiSelectComponent id="mtselement" dataSource={sportsData}  placeholder="Find a game" />
    );
}
ReactDOM.render(<App />, document.getElementById('sample'));

Configure the Popup List

By default, the width of the popup list automatically adjusts according to the MultiSelect input element’s width and the height of the popup list has ‘300px’.

You can also customize the suggestion list height and width using popupHeight  and popupWidth properties respectively.

In the following sample, popup list’s width and height are configured.

[Class-component]

import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    // define the array of data
    sportsData = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
    render() {
        return (
        // specifies the tag for render the MultiSelect component
        <MultiSelectComponent id="mtselement" dataSource={this.sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game"/>);
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {
    // define the array of data
    private sportsData: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby','Snooker', 'Tennis'];

    public render() {
        return (
              // specifies the tag for render the MultiSelect component
            <MultiSelectComponent id="mtselement" dataSource={this.sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game" />
            );
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));

[Functional-component]

import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
    // define the array of data
    let sportsData = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
    return (
    // specifies the tag for render the MultiSelect component
    <MultiSelectComponent id="mtselement" dataSource={sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    // define the array of data
    let sportsData: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby','Snooker', 'Tennis'];
    return (
    // specifies the tag for render the MultiSelect component
        <MultiSelectComponent id="mtselement" dataSource={sportsData} popupHeight="250px" popupWidth="250px" placeholder="Find a game" />
    );
}
ReactDOM.render(<App />, document.getElementById('sample'));

See Also