Introduction
The hybrid approach gives you the best of both worlds: the low interactivity of a single page app where needed, while keeping the simplicity and high performance advantages of server side rendered IHP apps. This guide will help you understand the best-practices of building hybrid applications with IHP and React.Adding React to your IHP Project
Add NodeJS to your project
Open your project’s Rebuild your local dev environment:After that, you have
flake.nix and add nodejs to packages:node and npm available in your project.NPM Init
Before installing dependencies, generate a Add the
package.json:Frontend/package.json and Frontend/package.lock to your git repository.Add Make tasks
Open your project’s To get the JS automatically built on deployment, append at the top of the file:
Makefile and append these tasks:Building a Real-time SPA with IHP DataSync
The following section assumes that your app already has a login as described in the Authentication section.
query, fetchOne or createRecord in JavaScript, similar to the Haskell side.
Example fetching posts:
Setting up the Schema
In this example we’re building a simple todo list.Create todos table
Create a new table Run
todos with the IHP Schema Designer:Update DB to insert the new table.Add the ihp_authenticated role
IHP DataSync uses Postgres Row Level Security. The default database role is the table owner and can access all rows. DataSync uses a second role called
ihp_authenticated.Open Config/Config.hs:Enable the DataSync controllers
Open Mount the controller:Open
Web/FrontController.hs and add imports:Web/Routes.hs and add:Load the JS SDK
Open Remove the
Web/View/Layout.hs and add these JavaScript files:helpers.js from your Layout - it typically causes troubles with React.Fetching Data
Open your app and try this in the JS console:Implementing the Todo List
Make it realtime
The
useQuery(query('todos')) call automatically sets up a subscription. Changes appear in real-time across all browser windows.Common DataSync Operations
Querying Records
Realtime Queries
Usesubscribe to keep the result set in sync:
Filtering
Fetching a Single Record
Ordering
Create Record
Create Many Records
Update Record By ID
Delete Record By ID
Using DataSync Without a Bundler
In yourWeb/View/Layout.hs file, add an importmap:
type="module" to your app.js script:
app.js, you can now import: