Repository navigation
Description
Description
I have some react wrappers which i have generated from my lit components. These work great in a pure client-side react app. However when using in a next.js app i hit a number of a problems.
Elliot on slack asked me to raise an issue here. Even though i think some of it is an issue on the next.js side, it might help in tracking your next integration story.
Steps to Reproduce
- Write this code
import React from "react";
import dynamic from "next/dynamic";
// @org/react contains lit-labs/react wrappers...
// (1) ts error
const MyInput = dynamic(() => import("@org/react").then(({ Input }) => Input), {
ssr: false,
});
export const Page = () => {
const ref = React.useRef();
// (2) ref issue
const focusInput = () => ref.current.focus();
return (
<>
<MyInput ref={ref} />
<button onClick={focusInput}>Click me</button>
</>
);
};- See this output...
(1) TS complains about the type of the react wrapper component. To fix this I had to cast to any and then back to my actual type. I created a helper function like:
type ComponentModule = typeof import("@org/react");
function createDynamic<
K extends keyof ComponentsModule,
T extends ComponentsModule[K]
>(component: K) {
return dynamic(
() => import("@org/react").then((module) => module[component] as any),
{ ssr: false }
) as T;
}
const MyInput = createDynamic("Input");(2) when clicking the button you will get an error because the ref is not pointing to the underlying custom-element as you would expect, but rather (i think) the return type of dynamic.
I was able to work around this by adding some indirection and threading through a ref myself:
type ComponentsModule = typeof import("@org/react")
function createDynamic<
K extends keyof ComponentsModule,
T extends ComponentsModule[K]
>(component: K) {
const Wrapped = dynamic(
() =>
import("@org/react").then(module => {
const Component = module[component] as any
// @ts-expect-error innerRef is unknown
const Wrapped = ({ innerRef, ...props }) => <Component ref={innerRef} {...props} />
Wrapped.displayName = `Wrapped${component}`
return Wrapped as any
}),
{ ssr: false }
)
// @ts-expect-error innerRef is unknown
const Forwarded = React.forwardRef((props, ref) => <Wrapped {...props} innerRef={ref} />)
Forwarded.displayName = `Forwarded${component}`
return Forwarded as T
}
const MyInput = createDynamic("Input")This is of course, horrible. I tried various other combinations of forwardRef and/or custom ref properties, but i coudln't get it to work. I even had to suppress some TS issues.
This got so complex, i had to add next.js specific integration to my react package as it would be very difficult to document these issues in such a way that devs consuming the components would understand. It was easier to just give them something they can import for next.js only
Expected Results
(1) no TS errors
(2) refs should work out of the box
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fields✅ Done
Activity
[-][lit-labs/react] Issues using lit-labs wrappers in Next.js[/-][+][labs/react] Issues using lit-labs wrappers in Next.js[/+]taylor-vann commentedon Sep 30, 2022
Hey @WickyNilliams #3163 now exports a type
ReactWebComponentthat should help TS better understand what's returned from createComponent.I'm curious if this helps alleviate type woes in an SSR scenario like NextJS.
I'd also like to work towards better SSR support for the
@lit-labs/reactpackage. So any requests or advice? I'm all ears :)WickyNilliams commentedon Sep 30, 2022
Hey thanks for the ping! I'll check this out when I'm back in work next week 🙏
jianliao commentedon Apr 6, 2023
Hi @taylor-vann,
I came across this issue created by @WickyNilliams and found that my target is similar, but I'm using the latest Next.js canary release and the latest @lit-labs/react. I've created a repository to reproduce the issue and tried two different approaches.
Approach 1
Code Reference
Approach 2
Code Reference
Both solutions have no TypeScript errors, but Approach 2 fails during Next.js static export (aka
next build). The problem is that both approaches result in the following runtime warning, and the ref remains null:I hope this information helps in finding a more suitable solution for integrating @lit-labs/react with Next.js. Any suggestions or advice would be appreciated.
jianliao commentedon Apr 6, 2023
Here are some resources that helped me develop the above solutions:
jianliao commentedon Apr 6, 2023
I believe that Attempt - 1 should ideally be the optimal solution, as the React component returned by createComponent(...) is already wrapped with React.forwardRef(...). Therefore, it shouldn't be necessary to wrap it again as in Attempt - 2.
steveworkman commentedon May 12, 2023
+1
Taking a wrapped react component I get this error trying to apply a ref
error:
augustjk commentedon Sep 26, 2023
This is an upstream issue in Next.js: vercel/next.js#40769
There's a PR linked to the issue that should fix this.
There probably isn't much more we can do on our end. We shouldn't have any type errors with
refon our components if therefis typed to be an instance of the original custom element class, but usage withnext/dynamichas runtime problems. Workaround is to wrap with a component that takes the ref in a different prop name likeforwardedRef.Thanks @AndrewJakubowicz for the excellent repro and investigation.