Skip to content

Instantly share code, notes, and snippets.

@lmmx
Last active June 28, 2024 20:11
Show Gist options
  • Save lmmx/59255d34fb3e84d30afd1da2d88e241f to your computer and use it in GitHub Desktop.
Save lmmx/59255d34fb3e84d30afd1da2d88e241f to your computer and use it in GitHub Desktop.
HTMX React app (combo) demo: random joke generator
import React, { useEffect, useRef } from 'react';
import htmx from 'htmx.org';
function HTMXComponent() {
const divRef = useRef(null);
useEffect(() => {
// Initialize HTMX on the component
htmx.process(divRef.current);
// Cleanup function
return () => {
htmx.trigger(divRef.current, 'htmx:beforeCleanup');
htmx.remove(divRef.current);
};
}, []);
return (
<div ref={divref}>
<button
hx-get="https://api.chucknorris.io/jokes/random"
hx-target="#joke-result"
hx-trigger="click"
hx-indicator="#loading"
>
Get Chuck Norris Joke
</button>
<div id="joke-result"></div>
<div id="loading" class="htmx-indicator">Loading...</div>
</div>
);
}
function App() {
return (
<div>
<h1>React App with HTMX Integration</h1>
<HTMXComponent />
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment