-
-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Events not working #94
Comments
This is the correct behaviour. |
https://preactjs.com/guide/v10/api-reference/#hydrate how is https://preactjs.com/guide/v10/server-side-rendering/ the official docs recommend this package, but do not describe how to use this package with hydrate to wire up events? |
@brandonros Good point we should expand our docs there. Here is an example that demonstrates how both libraries are used together. Basically the server sends the stringified HTML to the client and the client makes that interactive with // Dummy server:
const http = require('http');
const { h } = require('preact');
function App() {
return <button onClick={() => console.log("click")}>click me</button>
}
const server = http.createServer((req, res) => {
const app = renderToString(<App />);
const html = `<!DOCTYPE html>
<html>
<body>
<div id="app">${html}</div>
<script src="path/to/my-app.js"></script>
</body>
</html>`
res.end(html);
}).listen(3000); And // Run in the browser
import { h, hydrate } from 'preact';
function App() {
return <button onClick={() => console.log("click")}>click me</button>
}
// Does (nearly) no diffing, only attaches events
hydrate(<App />, document.getElementById("app")); |
@marvinhagemeister I am encountering some challenges in this endeavor, particularly regarding the integration of a "clientLoad" prop to compile and send dependencies to the static files for client-side loading of events. Despite my efforts, I am still facing difficulties in achieving the desired functionality
While I appreciate the solutions provided thus far, I have realized that I am not passing any dependencies to the client, which is essential for the loading of preact dependencies. This has led me to explore alternative solutions, but I find myself struggling to simplify the process. My goal is to avoid serving the entire project to the client and instead focus on executing certain components dedicated to client-side functionality directly on the client. Attached is the project I am currently experimenting with using preact. Although I am aware that React offers solutions such as "use client," I have reservations about using React due to its heavy nature and the multitude of dependencies it entails. I am considering modifying Preact and Preact/serverToString slightly to achieve this goal, not with 'use client' as React does. I am not fond of adding arbitrary strings to the code. Instead, I am exploring the possibility of using , similar to what is done in Astro. If successful, I intend to share my progress through a pull request for your review and feedback. If unsuccessful, there will be no pull request :P (sorry for the mail format XD, i love your project.) |
@MolikoDeveloper That's exciting! You're pretty much describing the Islands architecture on which Astro is also based on to a tee. This approach typically requires the following things:
The implementation chosen in Fresh to make this work is a little complex, because I went with comment markers and it also supports passing server rendered JSX to islands among a few other features unique to Fresh. Might be worth a look as an inspiration for your project. |
It seems DOM events are not working when using
preact-render-to-string
.Preact Version: 8.4.2
PreactRenderToString Version: 4.1.0
The text was updated successfully, but these errors were encountered: