What is HTMX?
HTMX allows you to access modern browser features directly from HTML, making it easier to build interactive web applications with less JavaScript.Step 1: Adding HTMX to Your Project
First, you need to include the HTMX library in your project. You can do this by installing the app HTMX to your site in the deco Admin.
Step 2: Making the Preact version of the Counter
Let’s create a Preact component for the counter to see the difference compared to a HTMX version:useState
hook to manage the counter state and the
onClick
event to update the counter when the buttons are clicked.
Step 3: Making the HTMX version of the Counter
In the HTMX version, we will no longer use theuseState
hook or the onClick
event. When using HTMX, we need to have one route for each UI state of the site,
so we will make a request to the server to update the counter state.
That’s when the useSection
hook comes in
handy. This hook automatically creates routes for rendering your UI states
without requiring developers to handle routing manually.
Let’s see what changes in the HTMX version:

