Hacker News new | ask | show | jobs
by djangpy 1037 days ago
Can someone explain to me how state management is done in HTMX? I'm a native Django developer, and I really do like HTMX and wrote my own personal website with it (nothing special, a digital garden). But for my startup, I chose react, and it has pretty much become a SPA, where I need to keep track of so much state. Now, I would love to transition to HTMX, because I find Django a lot more ergonomic to work with. But I couldn't imagine not running into issue managing state.
2 comments

I don't think there is a single answer to state management with htmx as in "put your key-value pairs here“. Instead the page depicts the application state and offers ways to change it or to change what is depicted each time updating parts of the page. So the state is what's on the page so to say. However some options are: 1. you can have some form of state in your url by having every user actions change a param, returning the updated page with updated links/forms. 2. You could keep a session on the backend. Never used this myself, however Django which you speak of above should have support for sessions without a user/login. 3. you can keep state in the database if is important enough and you have a user. 4. htmx also sends a lot of js events which you can react to by using some plain Javascript yourself. You can also send your own events and have htmx listen on them to have different components react to a page/state update elsewhere with their own page update.
State management is done in whatever day your backend does it. HTMX is mostly concerned with sending requests to the backend and shoving the HTML response in the right place in the DOM