DevExtreme React - Local Array
To bind a UI component to a local array, pass this array to the UI component's dataSource property. We recommend that you also set the keyExpr property OR the valueExpr and displayExpr properties, depending on your UI component:
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import DataGrid from 'devextreme-react/data-grid';
- import SelectBox from 'devextreme-react/select-box';
- import service from './data.js';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.employees = service.getEmployees();
- }
- getDisplayExpr(item) {
- return item && item.FirstName + ' ' + item.LastName;
- }
- render() {
- return (
- <React.Fragment>
- <DataGrid
- dataSource={this.employees}
- keyExpr="ID"
- />
- <SelectBox
- dataSource={this.employees}
- valueExpr="ID"
- displayExpr={this.getDisplayExpr}
- />
- </React.Fragment>
- );
- }
- }
- export default App;
- const employees = [
- { ID: 1, FirstName: 'Sandra', LastName: 'Johnson' },
- { ID: 2, FirstName: 'James', LastName: 'Scott' },
- { ID: 3, FirstName: 'Nancy', LastName: 'Smith' }
- ];
- export default {
- getEmployees() {
- return employees;
- }
- }
If you plan to update the data or need to handle data-related events, wrap the array in an ArrayStore. You can use the store's key property instead of the UI component's keyExpr or valueExpr. You can further wrap the ArrayStore in a DataSource if you need to filter, sort, group, and otherwise shape the data.
The following example declares an ArrayStore, wraps it in a DataSource, and binds the DataGrid and SelectBox UI components to this DataSource:
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import DataGrid from 'devextreme-react/data-grid';
- import SelectBox from 'devextreme-react/select-box';
- import ArrayStore from 'devextreme/data/array_store';
- import DataSource from 'devextreme/data/data_source';
- import service from './data.js';
- const employeesStore = new ArrayStore({
- data: service.getEmployees(),
- key: 'ID',
- onLoaded: function() {
- // ...
- }
- });
- const employeesDataSource = new DataSource({
- store: employeesStore,
- sort: 'LastName'
- });
- class App extends React.Component {
- getDisplayExpr(item) {
- return item && item.FirstName + ' ' + item.LastName;
- }
- render() {
- return (
- <React.Fragment>
- <DataGrid
- dataSource={employeesDataSource}
- />
- <SelectBox
- dataSource={this.employees}
- displayExpr={this.getDisplayExpr}
- />
- </React.Fragment>
- );
- }
- }
- export default App;
If you have technical questions, please create a support ticket in the DevExpress Support Center.