jeasx / jsx-async-runtime

An asynchronous JSX runtime without dependencies to be used as server-side template engine.
Other
10 stars 1 forks source link

Render option to bind a `this` #1

Closed pauleveritt closed 2 months ago

pauleveritt commented 2 months ago

I've been enjoying using jsx-async-runtime with 11ty. One pattern I'd be interested in: binding the original this to each subcomponent rendering. This would help avoid prop drilling. Let me explain.

In 11ty, the "template" renders a page. The template has some state bound to it using .bind, which means templates have access to configured functions and data.

Shortcodes (i.e. subcomponents) also have a bound this.

While I have access to the Eleventy-provide this in the original template, I don't for any subcomponents. This means I need to pass the universe down through props.

It would be nice if renderToString allowed an option bind=this. Then, each subcomponent rendering would do a bind().

jablonski commented 2 months ago

Can you provide a minimal example project which I can use to test the implementation?

pauleveritt commented 2 months ago

Would it help if I had a repo showing this binding idea using preact-render-to-string?

jablonski commented 2 months ago

Yes, any example would help.

pauleveritt commented 2 months ago

Sorry for the delay. Here's a tiny repo showing how it works with Preact and 11ty.

This might be easier: a 2m30s video walkthrough.

jablonski commented 2 months ago

The walkthrough helps, thanks! The repo seems to be private, maybe you can change access rights.

pauleveritt commented 2 months ago

@jablonski My apologies! Fixed on the repo.

jablonski commented 2 months ago

@pauleveritt Thanks, I've got the idea and have a working implementation now. I'm going to do some more tests tomorrow before pushing a release. But I'm quite confident that it is working as expected.

jablonski commented 2 months ago

Here is an example how to bind the 11ty-context in your project:

const result = await jsxToString.call({ context: data }, content);

jablonski commented 2 months ago

The changes are published in 0.4.0. Works as expected in 11ty and is a cool improvement for Jeasx (https://www.jeasx.dev/), too. Thanks a lot for raising the issue.

pauleveritt commented 2 months ago

Thanks a bunch for doing this. I'll try it today.

pauleveritt commented 2 months ago

I have another feature kind of thing to ask about. It's not a bug. Should you open Discussions or is it ok for me to file a ticket?

jablonski commented 2 months ago

Ticket is fine.