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.
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 structureconst endpoint = '' // todo: insert here Method name of your API-endpoint//...// Auth context:auth.user // returns user IDauth.sessionID // returns user session IDauth.isAutorised() // returns true if user is authorisedauth.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 payloadfunction 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})}//...
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:
Have a look at simple app MyMovieList (and its code on GitHub)
Here is a live-demo of developing process: