Skip to content
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

Add support for Hono Client Components #448

Open
gaetan-puleo opened this issue May 24, 2024 · 10 comments
Open

Add support for Hono Client Components #448

gaetan-puleo opened this issue May 24, 2024 · 10 comments
Labels
bug Something isn't working feature

Comments

@gaetan-puleo
Copy link

Hono has a client components system, and is very similar to React but much more smaller.

https://hono.dev/guides/jsx-dom#client-components

@molefrog
Copy link
Owner

Thanks for the suggestion. I am not familiar with Hono's components yet, could you maybe write your ideas on how this could be implemented?

@gaetan-puleo
Copy link
Author

Thanks for the interest.
Hono/jsx/dom (hono client components) export the same hooks than react.

We could try to mimic this file https://github.com/molefrog/wouter/blob/v3/packages/wouter/src/react-deps.js (or the preact file) with the same hooks and export everything in wouter/hono?

If I take the import coming from React and used in react-deps.

export {
  useRef,
  useState,
  useContext,
  createContext,
  isValidElement,
  cloneElement,
  createElement,
  Fragment,
  forwardRef,
} from "react";

All the thing exist in Hono.

I think we should be able to make wouter works for hono.

Here the methods coming from Hono.

image

(Sorry for my bad English)

@molefrog
Copy link
Owner

molefrog commented May 30, 2024

Thanks, I'll think more about this, right now I don't see a straightforward solution. Maybe you can try importing wouter from a CDN (like esm.sh) with react dependency overriden? Similarly, to what you would do to use convert a React library to Preact?

P.S. Your English is fine! I am a non-native speaker myself.

@gaetan-puleo
Copy link
Author

It's not urgent on my side, I use react right now, I'll migrate to hono/jsx/dom when a solution is ready.

When I have some time, I will try your solution. (I didn't know about esm.sh, thank you)

@molefrog molefrog changed the title Add support for hono Add support for Hono Client Components Jun 4, 2024
@gaetan-puleo
Copy link
Author

Hi @molefrog there is a fork of wouter with hono support but the ssrPath is not working.
Here the repo https://github.com/apvarun/wouter-hono.

I created an issue here.
apvarun/wouter-hono#1

But I still think the hono package could be part of the wouter package.

@molefrog
Copy link
Owner

Alright, I did some brief digging and it looks like the problem in this line

return h(RouterCtx.Provider, { value, children });

wouter uses createElement to construct a provider element and passes children as a prop, however Hono does not support this and expects children to be the third argument. Hence, nothing gets rendered.
We need to investigate if this is expected behaviour or something that Hono does not support yet.

@molefrog
Copy link
Owner

I run a quick test and it worked, however there is another issue: Hono can't render components that return null.

@gaetan-puleo
Copy link
Author

I run a quick test and it worked, however there is another issue: Hono can't render components that return null.

Can you add more info about the test ?

I can create an issue about this point here if needed https://github.com/honojs

@molefrog
Copy link
Owner

Sure, here is what you can test (these are separate cases):

  1. Create a Hono app with Client Components. Make a component that renders null and render it in your app. In React, this will render nothing, while in Hono it throws an error.
  2. Call createElement method from hono/jsx and try to make a new JSX element with children provided as a prop:
import { createElement } from "hono/jsx"

const el = createElement("div", { children: <a href="/">Link</a> })
// expected behaviour is the same as in React:
// div with a link in it, instead it just creates a `div`

The fix is to pass children as a third argument. We can fix this in wouter's source though, but I think it would make sense to have this behaviour similar to React's.

@molefrog
Copy link
Owner

molefrog commented Sep 2, 2024

Looks like 1) is no longer an issue, it was fixed here honojs/hono#3241

Could you try to update your example and see if this error still occurs with the latest version of Hono?

@molefrog molefrog added the bug Something isn't working label Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature
Projects
None yet
Development

No branches or pull requests

2 participants