Oliver's Blog

DevExtreme React Grid - Basic Setup

This post is part of a series about the DevExtreme React Grid. You can find the introduction and overview to the post series by following this link.

Getting started

Most applications that use React are developed with some kind of build environment. Many introductions and tutorials exist for React, including the one on the project website, so I’m not going to reproduce this part here.

As a quick and easy way tool to create a new React based app, using a standard build environment, I recommend create-react-app.

On the basis of a React application, we have a short getting started section on the first page of the React Grid documentation. I didn’t have any trouble getting the React Grid to work correctly following these instructions, but please let me know in case you encounter any issues!

For the purposes of this blog series, I decided to prepare a few examples in CodePen. In each of these “Pens”, you can see in the JavaScript page of the Settings how I’ve added the required references. This is not a generally recommended approach for real-world applications! For demos however, it is brilliant since you can play with my samples directly.

The most basic Grid setup

My first example shows a realistic minimum configuration. You can see the Pen a few paragraphs below and I’ll explain the code of the demo here.

At the top of the file, you see the one line of code that would be different if I weren’t working in a Pen:

const { Grid, TableView, TableHeaderRow } = DevExpress.DXReactGridBootstrap3;

I included a comment in the Pen to point out the difference. Whichever syntax you use, you end up with the three elements Grid, TableView and TableHeaderRow, which are now available for use in the rest of the code. The TableHeaderRow is technically optional, but I decided to include it in my miminum setup to create a recognizable grid appearance.

class App extends React.PureComponent {

This line creates a new component called App. It is derived from the React PureComponent, the recommended base class for most React components since React version 15.3.

In the constructor of the component, I preset the component state to include a column definition and some sample data:

constructor(props) {
  super(props);

  this.state = {
    columns: [
      { name: 'name', title: 'Name' },
      { name: 'artist', title: 'Artist'},
      { name: 'year', title: 'Year' }
    ],
    rows: [ ... ]
  };
}

Of course your data would probably come from some other source in most real-world applications, but it is easy to see that you would only need to initialize the state in some other way to apply any real-world data retrieval technique you use.

The render function of the component retrieves the two lists columns and rows from state and then returns a JSX structure that renders the three components I loaded at the start of the code. The column and row content is included by means of the curly brace expressions, and thereby passed to the Grid element.

render() {
  const { rows, columns } = this.state;

  return (
    <Grid rows={rows} columns={columns}>
      <TableView />
      <TableHeaderRow />
    </Grid>
  );
}

The TableView and TableHeaderRow are plugins to the Grid, which is why they appear nested as children.

Note that the order of plugin elements is important! The TableHeaderRow could not possibly do its job if a TableView weren’t available first. Keep this in mind for all plugins: order is relevant. Our documentation pages define dependencies of each plugin, right at the top of each page.

The final line of this first demo renders the App component into the HTML page. The latter contains nothing but one div: <div id="app"></div>.

Here’s the CodePen that shows the steps of this first example. Please note that you can click the Edit on CodePen link in the top right corner to bring up the full editor, where you can also see project settings including dependencies.

Click here to return to the blog series overview

Published Jul 04 2017, 11:24 AM by
Filed under: ,
Bookmark and Share

Comments

No Comments
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners