React Boilerplate code

The way to skip infrastructure messing up building a React app🤘

Find the step-by-step manual and the boilerplate code on GitHub

Using Directual you can setup the whole backend for your React App in less than 15 minutes, and then, should any change in database or API required, you can make that change almost instantly.

React app with Directual backend scheme

User Interface (UI)

Feel free to build your UI components as you used to: classes, hooks, etc. And there, in your components you can use the following Directual features:

  • Authentication methods: login, logout, auth context, etc.;

  • Getting data from the database;

  • Posting data into the database.

Have a look at these code patterns:

import Directual from 'directual-api';
import { useAuth } from '../auth' // auth.js is a React-wrapper for directual-api authentication methods
//...
const api = new Directual({ apiHost: '/' })
const auth = useAuth();
//...
const dataStructure = '' // todo: insert here Sysname of your data structure
const endpoint = '' // todo: insert here Method name of your API-endpoint
//...
// Auth context:
auth.user // returns user ID
auth.sessionID // returns user session ID
auth.isAutorised() // returns true if user is authorised
auth.hasRole('role') // returns true if user.role == 'role' (see user management further)
//...
// GET request:
function getData() {
api
// Name of Data structure (table) in the Database
.structure(dataStructure)
// GET request + query params (sessionID)
.getData(endpoint, { sessionID: auth.sessionID})
// other possible query params: page, pageSize, sort and any custom parameter for Filtering
.then((response) => {
// handling response
})
.catch((e) => {
// handling errors
})
}
//...
// POST-request:
let payload = {} // Request payload
function postData() {
api
// Name of Data structure (table) in the Database
.structure(dataStructure)
// POST request + payload + query params
.setData(endpoint, payload, { sessionID: auth.sessionID })
.then((response) => {
// handling response
})
.catch((e) => {
// handling errors
})
}
//...

Directual React module

There are two useful files from Directual React Boilerplate: auth.js provides methods: .user, .sessionID, .isAutorised(), .hasRole(). setupProxy.js creates middleware proxy to directual-api, for resolving the problem linked with CORS. We need to install http-proxy-middleware.

Also you need to edit the .env file for connecting with Directual app: APP_ID=_YOUR_APP_ID_

You can find (or create) your App ID in API section:

Live-demo example

Have a look at simple app MyMovieList (and its code on GitHub)

Here is a live-demo of developing process: