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

Use client #12

Merged
merged 3 commits into from
Nov 14, 2023
Merged

Use client #12

merged 3 commits into from
Nov 14, 2023

Conversation

tkrotoff
Copy link
Contributor

To be compatible with React Server Components, the 'use client' directive is needed, see https://react.dev/reference/react/use-client

Problem: Microbundle and Rollup don't support 'use client'

Solution: given this source code is very small, use createElement and CJS directly.
Then no need for a bundler, the code is directly compatible with everything (CJS, ESM).

I carefully checked the example works, the tests pass and using my own app which uses this package, that nothing is broken.

Here the npm package it generates:

image

@@ -15,8 +17,7 @@ function DangerouslySetHtmlContent({ html, dangerouslySetInnerHTML, ...rest }) {
divRef.current.appendChild(slotHtml) // Append the new content
}, [html, divRef])

// eslint-disable-next-line react/react-in-jsx-scope
return <div {...rest} ref={divRef} />
return createElement('div', { ...rest, ref: divRef })
Copy link
Contributor Author

@tkrotoff tkrotoff Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@tkrotoff tkrotoff Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@christo-pr
Copy link
Owner

@tkrotoff Thanks a lot!!! this is really really helpful, so let me get this straight then, you're saying that we no longer need to build the code right? so a simple npm publish will upload the js file and the ts file and that's it correct?

@christo-pr christo-pr merged commit 9a2e8ba into christo-pr:main Nov 14, 2023
@tkrotoff tkrotoff deleted the use-client branch November 14, 2023 21:52
@tkrotoff
Copy link
Contributor Author

we no longer need to build the code right?

Yes that's the idea.

The only edge cases I can think of are:

  • Users of UMD, but you never exported UMD anyway
  • Users of ESM directly in the browser, I don't think it's handled using microbundle + the way React exports itself

@christo-pr
Copy link
Owner

Yeah that why I though, well I just bump the version and publish it, I wonder if the 'use client' at the top will affect any usage of this for older version of react, I guess we'll see on the issues, thanks again @tkrotoff really appreciate the help! 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants