Last active
June 28, 2024 20:11
-
-
Save lmmx/59255d34fb3e84d30afd1da2d88e241f to your computer and use it in GitHub Desktop.
HTMX React app (combo) demo: random joke generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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