How to Create a Basic CRUD App with Preact?
In this guide, you'll develop a CRUD (create, read, update, and delete) application using Preact.
We will walk you through building a system that handles basic data operations, from initializing a Back4app project named Basic-CRUD-App-Preact to managing data effortlessly. This project provides a robust backend and a sleek, minimal frontend built with Preact.
We'll begin by setting up a new project on Back4app, then craft an efficient database schema—either manually or with the help of the Back4app AI Assistant. This design will support all essential CRUD functionalities.
Next, we introduce the Back4app Admin App, a drag-and-drop interface that simplifies data management. You can quickly add, modify, or remove records using this tool.
Finally, you will connect your Preact frontend to Back4app using REST/GraphQL , ensuring your backend is secure with advanced access rules.
By the end of this tutorial, you'll have a production-ready web application featuring user authentication and robust data management—all built with Preact and powered by Back4app.
- Master building CRUD applications that efficiently manage data using a dependable backend.
- Learn to design a scalable data model and integrate it with a Preact frontend for an optimal user experience.
- Discover how the drag-and-drop Back4app Admin App simplifies create, read, update, and delete operations.
- Understand deployment strategies, including containerization with Docker, to quickly launch your application.
Before starting, please make sure you have:
- A Back4app account and an initialized project. For guidance, check out Getting Started with Back4app.
- A Preact development environment. Use tools like Preact CLI or equivalent, ensuring Node.js (v14 or later) is installed.
- Sign in to your Back4app account.
- Select the “New App” option on your dashboard.
- Name your project: Basic-CRUD-App-Preact and follow the setup prompts.

After creation, your project will be visible on your Back4app dashboard, laying the groundwork for backend configuration.
For our CRUD app, you'll need several collections. Below are examples that outline the collections and fields essential for supporting CRUD operations.
This collection keeps details about each item.
Field | Data Type | Purpose |
---|---|---|
_id | ObjectId | Auto-generated primary key. |
title | String | The name of the item. |
description | String | A short description of the item. |
created_at | Date | The creation timestamp. |
updated_at | Date | Last update timestamp. |
This holds user credentials and profile details.
Field | Data Type | Purpose |
---|---|---|
_id | ObjectId | Auto-generated primary key. |
username | String | Unique username. |
String | Unique email address. | |
password_hash | String | Hashed password for authentication. |
created_at | Date | Account creation timestamp. |
updated_at | Date | Last account update timestamp. |
You can create these collections manually via the Back4app dashboard by adding a new class for each collection and defining the appropriate fields.

You can add columns by selecting a data type, naming the column, and setting default values and required flags.

The AI Assistant available in Back4app can auto-generate your schema based on a prompt describing your collections and fields. This feature expedites the setup and ensures consistency.
- Open the AI Assistant from your Back4app dashboard.
- Input a detailed description of your data model.
- Review the auto-generated collections and field definitions, then apply them to your project.
This approach saves time and guarantees a well-structured schema.
The Back4app Admin App provides a no-code interface to manage your backend data effortlessly. Its drag-and-drop functionality enables you to perform CRUD operations without hassle.
- Go to the “More” menu in your Back4app dashboard.
- Select “Admin App” and click “Enable Admin App.”
- Set up your first admin user, which will also configure roles like B4aAdminUser and system collections.

After activation, log in to the Admin App to manage your data.

Within the interface you can:
- Add Records: Use the “Add Record” option in any collection (e.g., Items) to create new entries.
- View and Edit Records: Select a record to see its details or modify its fields.
- Remove Records: Delete entries that are no longer needed.
This intuitive tool enhances user experience by streamlining data management.
With the backend in place, it’s time to integrate your Preact frontend.
- Install the Parse SDK:
- Set up Parse in your Preact project. Create a file (e.g., src/parseConfig.js):
- Integrate Parse in a Preact component. For instance, build a component to fetch and display items:
If the Parse SDK is not viable, use REST or GraphQL for CRUD operations. For instance, to fetch items via REST:
Integrate these API calls within your Preact components as required.
Secure your data by assigning ACLs to your objects. For instance, to create a private item:
Within the Back4app dashboard, adjust CLPs for each collection to restrict access to authenticated or authorized users only.
Back4app utilizes Parse’s User class for managing authentication. In your Preact application, manage user sign-up and login as follows:
A similar method can be used for login and managing sessions. Additional features like social authentication and password recovery can be set up via the Back4app dashboard.
Back4app’s Web Deployment feature lets you host your Preact application by linking a GitHub repository. In this part, you'll prepare your production build, commit your code, integrate with Web Deployment, and deploy your site.
- Open your project directory in a terminal.
- Run the build command:
This generates a build folder with optimized static assets (HTML, JS, CSS, images). 3. Confirm that the build folder includes an index.html file and necessary assets.
Your repository should contain the complete Preact frontend source code. A sample directory structure:
src/parseConfig.js
src/App.js
- Initialize a Git repository (if not already done):
- Stage your source files:
- Commit your changes:
- Create a new GitHub repository, for example, Basic-CRUD-App-Preact-Frontend.
- Push your code:
- Go to the Web Deployment section in your Back4app dashboard, open your project (Basic-CRUD-App-Preact), and select Web Deployment.
- Link your GitHub account as prompted, so Back4app can access your repository.
- Select the repository (e.g., Basic-CRUD-App-Preact-Frontend) and choose the appropriate branch (e.g., main).
Specify additional settings:
- Build Command: If a pre-built build folder is absent, set the build command (e.g., npm run build). Back4app will run this command during deployment.
- Output Directory: Define the output folder as build so that Back4app identifies the static files.
- Environment Variables: Add any necessary API keys or settings required by your application.
If you prefer Docker, include a Dockerfile in your repository similar to:
Select the Docker deployment option in your Back4app Web Deployment settings.
- Click the Deploy button once configuration is complete.
- Monitor the build process as Back4app fetches your code, builds, and deploys your application.
- Once finished, Back4app will provide a URL where your application is hosted.
- Visit the provided URL to see your live site.
- Test all functionalities to ensure every feature, including routes and assets, loads correctly.
- If issues occur, review browser console logs and deployment logs in Back4app.
Great work! You've successfully developed a full CRUD application using Preact and Back4app.
You set up a project named Basic-CRUD-App-Preact, defined robust database collections, and used the Admin App for efficient data management.
You then connected your Preact frontend to your backend and implemented strict access controls.
What’s Next?
- Enhance your Preact app with additional features such as detailed views, search capabilities, and real-time updates.
- Incorporate more advanced backend logic like Cloud Functions or third-party API integrations.
- Consult the Back4app documentation and other resources for deeper insights into optimization and custom configurations.
By following this tutorial, you’ve gained the skills to build a scalable, secure CRUD backend for your Preact applications using Back4app. Enjoy coding and exploring new features!
