Search results

Getting started with React ListView component

20 Apr 2021 / 3 minutes to read

The following section explains the required steps to build the simple ListView component with its basic usage in step by step procedure.

Dependencies

Install the below required dependent packages to render the ListView component.

Copied to clipboard
+-- @syncfusion/ej2-react-lists
    |-- @syncfusion/ej2-react-base
    |-- @syncfusion/ej2-lists
        |-- @syncfusion/ej2-base
        |-- @syncfusion/ej2-data

Installation and Configuration

You can use create-react-app to setup the applications.

To install create-react-app run the following command.

Copied to clipboard
npm install -g create-react-app

Start a new project using create-react-app command as follows

Copied to clipboard
create-react-app quickstart --typescript

cd quickstart
Copied to clipboard
create-react-app quickstart

cd quickstart

Adding Syncfusion packages

All the available Essential JS 2 packages are published in npmjs.com public registry. Now, we are going to render ListView component from these packages.

To install ListView component, use the following command.

Copied to clipboard
npm install @syncfusion/ej2-react-lists --save

The above command installs ListView dependencies which are required to render the component in the React environment.

Adding ListView component

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

Copied to clipboard
import * as React from 'react';
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
import './App.css';

export default class App extends React.Component<{}, {}> {
  render() {
    return (
      //specifies the tag to render the ListView component
      <ListViewComponent id='list' />
    );
  }
}
Copied to clipboard
import * as React from 'react';
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
import './App.css';
export default class App extends React.Component {
    render() {
        return (
        //specifies the tag to render the ListView component
        <ListViewComponent id='list'/>);
    }
}

Adding CSS Reference

Import ListView component required theme references at the top of src/App.css.

Copied to clipboard
/* import the ListView dependency styles */

@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-react-lists/styles/material.css";
  • If you are using CheckList behavior in ListView, we need to add Button component’s styles as given below in src/App.css file
Copied to clipboard
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";

Bind dataSource

Populate the data in ListView using dataSource property. Here, an array of JSON values passed to ListView component.

Copied to clipboard
import * as React from 'react';
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
import './App.css';

export default class App extends React.Component<{}, {}> {
  // define the array of Json
  private arts: { [key: string]: string }[] = [
{ text: 'Artwork', id: '01' },
{ text: 'Abstract', id: '02' },
{ text: 'Modern Painting', id: '03' },
{ text: 'Ceramics', id: '04' },
{ text: 'Animation Art', id: '05' },
{ text: 'Oil Painting', id: '06' }];
  render() {
return (
  // specifies the tag to render the ListView component
  <ListViewComponent id="list" dataSource={this.arts} />
);
  }
}
Copied to clipboard
import * as React from 'react';
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
import './App.css';
export default class App extends React.Component {
    constructor() {
        super(...arguments);
        // define the array of Json
        this.arts = [
            { text: 'Artwork', id: '01' },
            { text: 'Abstract', id: '02' },
            { text: 'Modern Painting', id: '03' },
            { text: 'Ceramics', id: '04' },
            { text: 'Animation Art', id: '05' },
            { text: 'Oil Painting', id: '06' }
        ];
    }
    render() {
        return (
        // specifies the tag to render the ListView component
        <ListViewComponent id="list" dataSource={this.arts}/>);
    }
}

Running the application

Now use the npm start command to run the application in the browser.

Copied to clipboard
npm start
Source
Preview
index.tsx
index.html
index.css
index.jsx
Copied to clipboard
import * as React from 'react';
import * as ReactDOM from "react-dom";
import { ListViewComponent } from '@syncfusion/ej2-react-lists';

export default class App extends React.Component<{}, {}> {
  // define the array of Json
  private arts: { [key: string]: string }[] = [
{ text: 'Artwork', id: '01' },
{ text: 'Abstract', id: '02' },
{ text: 'Modern Painting', id: '03' },
{ text: 'Ceramics', id: '04' },
{ text: 'Animation Art', id: '05' },
{ text: 'Oil Painting', id: '06' }];
  render() {
return (
  // specifies the tag to render the ListView component
  <ListViewComponent id='list' dataSource={this.arts} ></ListViewComponent>
);
  }
}

ReactDOM.render(<App />, document.getElementById('element'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React ListView</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="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-lists/styles/material.css" rel="stylesheet" />
    <link href="index.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>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='element' style="margin:0 auto; max-width:400px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
#list {
  display: block;
  max-width: 400px;
  margin: auto;
  border: 1px solid #dddddd;
  border-radius: 3px;
}
Copied to clipboard
import * as React from 'react';
import * as ReactDOM from "react-dom";
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
export default class App extends React.Component {
    constructor() {
        super(...arguments);
        // define the array of Json
        this.arts = [
            { text: 'Artwork', id: '01' },
            { text: 'Abstract', id: '02' },
            { text: 'Modern Painting', id: '03' },
            { text: 'Ceramics', id: '04' },
            { text: 'Animation Art', id: '05' },
            { text: 'Oil Painting', id: '06' }
        ];
    }
    render() {
        return (
        // specifies the tag to render the ListView component
        <ListViewComponent id='list' dataSource={this.arts}></ListViewComponent>);
    }
}
ReactDOM.render(<App />, document.getElementById('element'));

See Also

Data Binding Types

ListView customization

Virtualization