Skip to main content

Integration with React

tip

You should be familiar with the basic concepts and patterns of React before reading this documentation. To refresh your knowledge, please refer to the React documentation.

DHTMLX Kanban is compatible with React. We have prepared code examples on how to use DHTMLX Kanban with React. For more information, refer to the corresponding Example on GitHub.

Creating a project

info

Before you start to create a new project, install Vite (optional) and Node.js.

You can create a basic React project or use React with Vite. Let's name the project as my-react-kanban-app:

npx create-react-app my-react-kanban-app

Installation of dependencies

Go to the new created app directory:

cd my-react-kanban-app

Install dependencies and start the dev server. For this, use a package manager:

  • if you use yarn, run the following commands:
yarn
yarn start
  • if you use npm, run the following commands:
npm install
npm run dev

The app should run on a localhost (for instance http://localhost:3000).

Creating Kanban

Now you should get the DHTMLX Kanban source code. First of all, stop the app and proceed with installing the Kanban package.

Step 1. Package installation

Download the trial Kanban package and follow steps mentioned in the README file. Note that trial Kanban is available 30 days only.

Step 2. Component creation

Now you need to create a React component, to add a Kanban into the application. Create a new file in the src/ directory and name it Kanban.jsx.

Importing source files

Open the Kanban.jsx file and import Kanban source files. Note that:

  • if you use PRO version and install the Kanban package from a local folder, the import paths look like this:
Kanban.jsx
import { Kanban, Toolbar } from 'dhx-kanban-package';
import 'dhx-kanban-package/dist/kanban.css';

Note that depending on the used package, the source files can be minified. In this case make sure that you are importing the CSS file as kanban.min.css.

  • if you use the trial version of Kanban, specify the following paths:
Kanban.jsx
import { Kanban, Toolbar } from '@dhx/trial-kanban';
import "@dhx/trial-kanban/dist/kanban.css";

In this tutorial you can see how to configure the trial version of Kanban.

Setting containers and adding Kanban with Toolbar

To display Kanban with Toolbar on the page, you need to create containers for Kanban and Toolbar, and initialize these components using the corresponding constructors:

Kanban.jsx
import { useEffect, useRef } from "react";
import { Kanban, Toolbar } from '@dhx/trial-kanban';
import '@dhx/trial-kanban/dist/kanban.css'; // include Kanban styles

export default function KanbanComponent(props) {
let toolbar_container = useRef(); // initialize container for Toolbar
let kanban_container = useRef(); // initialize container for Kanban

useEffect(() => {
// initialize the Kanban component
const kanban = new Kanban(kanban_container.current, {});

// initialize the Toolbar component
const toolbar = new Toolbar(toolbar_container.current, {
api: kanban.api, // provide Kanban inner API
// other configuration properties
});

return () => {
kanban.destructor(); // destruct Kanban
toolbar.destructor(); // destruct Toolbar
};
}, []);

return <div className="component_container">
<div ref={toolbar_container}></div>
<div ref={kanban_container} className="widget"></div>
</div>
}

Adding styles

To display Kanban correctly, you need to specify important styles for Kanban and its container in the main css file of the project:

index.css
/* specify styles for initial page */
html,
body,
#root {
height: 100%;
padding: 0;
margin: 0;
}

/* specify styles for Kanban and Toolbar container */
.component_container {
height: 100%;
margin: 0 auto;
}

/* specify styles for Kanban container */
.widget {
height: calc(100% - 56px);
}

Loading data

To add data into the Kanban, you need to provide a data set. You can create the data.js file in the src/ directory and add some data into it:

data.js
export function getData() {
const columns = [
{
label: "Backlog",
id: "backlog"
},
{
label: "In progress",
id: "inprogress"
},
// ...
];

const cards = [
{
id: 1,
label: "Integration with Angular/React",
priority: 1,
color: "#65D3B3",
start_date: new Date("01/07/2021"),
users: [3, 2],
column: "backlog",
type: "feature",
},
{
label: "Archive the cards/boards ",
priority: 3,
color: "#58C3FE",
users: [4],
progress: 1,
column: "backlog",
type: "feature",
},
// ...
];

const rows = [
{
label: "Feature",
id: "feature",
},
{
label: "Task",
id: "task",
}
];

return { columns, cards, rows };
}

Then open the App.js file and import data. After this you can pass data into the new created <Kanban/> components as props:

App.js
import Kanban from "./Kanban";
import { getData } from "./data";

function App() {
const { columns, cards, rows } = getData();
return <Kanban columns={columns} cards={cards} rows={rows} />;
}

export default App;

Go to the Kanban.jsx file and apply the passed props to the Kanban configuration object:

Kanban.jsx
import { useEffect, useRef } from "react";
import { Kanban, Toolbar } from "@dhx/trial-kanban";
import "@dhx/trial-kanban/dist/kanban.css";

export default function KanbanComponent(props) {
let kanban_container = useRef();
let toolbar_container = useRef();

useEffect(() => {
const kanban = new Kanban(kanban_container.current, {
columns: props.columns, // apply column data
cards: props.cards, // apply card data
rows: props.rows, // apply row data
rowKey: "type",
// other configuration properties
});

const toolbar = new Toolbar(toolbar_container.current, {
api: kanban.api,
// other configuration properties
});

return () => {
kanban.destructor();
toolbar.destructor();
};
}, []);

return <div className="component_container">
<div ref={toolbar_container}></div>
<div ref={kanban_container} className="widget"></div>
</div>
}

You can also use the parse() method inside the useEffect() method of React to load data into Kanban:

Kanban.jsx
import { useEffect, useRef } from "react";
import { Kanban, Toolbar } from "@dhx/trial-kanban";
import "@dhx/trial-kanban/dist/kanban.css";

export default function KanbanComponent(props) {
let kanban_container = useRef();
let toolbar_container = useRef();

let columns = props.columns; // data for columns
let cards = props.cards; // data for cards
let rows = props.rows; // data for rows

useEffect(() => {
const kanban = new Kanban(kanban_container.current, {
columns: [],
cards: [],
rows: [],
rowKey: "type",
// other configuration properties
});

const toolbar = new Toolbar(toolbar_container.current, {
api: kanban.api,
// other configuration properties
});

kanban.parse({ columns, cards, rows });

return () => {
kanban.destructor();
toolbar.destructor();
};
}, []);

return <div className="component_container">
<div ref={toolbar_container}></div>
<div ref={kanban_container} className="widget"></div>
</div>
}

The parse(data) method provides data reloading on each applied change.

Now the Kanban component is ready. When the element will be added to the page, it will initialize the Kanban with data. You can provide necessary configuration settings as well. Visit our Kanban API docs to check the full list of available properties.

Handling events

When a user makes some action in the Kanban, it invokes an event. You can use these events to detect the action and run the desired code for it. See the full list of events.

Open Kanban.jsx and complete the useEffect() method in the following way:

Kanban.jsx
// ...
useEffect(() => {
const kanban = new Kanban(kanban_container.current, {});

kanban.api.on("add-card", (obj) => {
console.log(obj.columnId);
});

return () => {
kanban.destructor();
};
}, []);
// ...

After that, you can start the app to see Kanban loaded with data on a page.

Kanban initialization

Now you know how to integrate DHTMLX Kanban with React. You can customize the code according to your specific requirements. The final advanced example you can find on GitHub.