DevExtreme React - Best Practices

Using Unions Instead of Base Classes

Inheritance is a part of internal implementation. You should not rely on the knowledge of relationships between base classes and their descendants. For generalization, use a union to define your own type.

For example, if you need a type for properties of multiple editor components, define the following union:

App.tsx
  • type EditorProps = TextAreaTypes.Properties | TextBoxTypes.Properties | AutocompleteTypes.Properties | LookupTypes.Properties;

Generic Parameters

Some types are generic. This is mostly the case for data-aware component types, such as DataGrid:

App.tsx
  • import { useRef } from 'react';
  • import { type Employee, employees as dataSource } from './data';
  • import DataGrid, { type DataGridTypes, DataGridRef } from 'devextreme-react/data-grid';
  • import Button from 'devextreme-react/button';
  •  
  • function onEditorPreparing(e: DataGridTypes.EditorPreparingEvent<Employee, number>) {
  • if (e.dataField === 'LastName' && e.parentType === 'dataRow') {
  • e.editorOptions.disabled = e.row?.data && e.row?.data.FirstName === '';
  • }
  • }
  •  
  • function App() {
  • const dataGrid = useRef<DataGridRef<Employee, number>>(null);
  •  
  • const onButtonClick = () => {
  • const dataGridInstance = dataGrid.current!.instance();
  • dataGridInstance.option("disabled", true);
  • }
  •  
  • return (
  • <>
  • <Button
  • text={"Disable DataGrid"}
  • onClick={onButtonClick}
  • />
  • <DataGrid<Employee, number>
  • ref={dataGrid}
  • onEditorPreparing={onEditorPreparing}
  • dataSource={dataSource}>
  • </DataGrid>
  • </>
  • );
  • }
  •  
  • export default App;

Types for Templates

Templates can include complex template data as an argument.

To define types for such templates, import the ComponentTypes declaration and find the type that ends with TemplateData:

App.tsx
  • import DataGrid, { type DataGridTypes } from 'devextreme-react/data-grid';
  • import Calendar, { type CalendarTypes } from 'devextreme-react/calendar';
  •  
  • // A rendering function
  • const gridCell = (data: DataGridTypes.ColumnCellTemplateData) => {
  • return <span>Custom text</span>;
  • }
  •  
  • // A component
  • const CustomCell = ({ data }: { data: CalendarTypes.CellTemplateData }) => {
  • const { text, view } = data;
  • return <span>{ text }</span>;
  • }
  •  
  • export default App() {
  • return (
  • <React.Fragment>
  • <DataGrid>
  • <Column cellRender={gridCell} />
  • </DataGrid>
  • <Calendar cellComponent={CustomCell} />
  • </React.Fragment>
  • );
  • }