Next.js Discord

Discord Forum

How can i set `ontouchstart=""` attribute to html element on server in nextjs?

Answered
Nebelung posted this in #help-forum
Open in Discord
Avatar
NebelungOP
Third-party library sets this attribute on first render on client and it causes hydration errors. I want to set it on server side so they would match
Answered by B33fb0n3
imagine it would be possible (appart from what you said here: https://nextjs-forum.com/post/1313889801510060124#message-1313890820046458901) and the client now set's it to a different value, then what happens: hydration error. So it wouldn't resolve your hydration error when you just set it.

Import your client component with disabled SSR and you are good to go:
const ComponentC = dynamic(() => import('../components/C'), { ssr: false })
View full answer

24 Replies

Avatar
@Nebelung Third-party library sets this attribute on first render on client and it causes hydration errors. I want to set it on server side so they would match
Avatar
you can set it by adding it directly to your element like:
<div onTouchStart={() => {}}/>
Avatar
@B33fb0n3 you can set it by adding it directly to your element like: tsx <div onTouchStart={() => {}}/>
Avatar
NebelungOP
This value is not the same as "". It produces different HTML
Avatar
@Nebelung This value is not the same as `""`. It produces different HTML
Avatar
then set it to:
<div onTouchStart=""/>
Btw this wouldn't resolve your issue
Avatar
@B33fb0n3 then set it to: <div onTouchStart=""/>
Avatar
NebelungOP
And this is not valid react code and produces errors on render about mismatched types
Avatar
@B33fb0n3 Btw this wouldn't resolve your issue
Avatar
NebelungOP
Why though?
Avatar
@Nebelung Why though?
Avatar
imagine it would be possible (appart from what you said here: https://nextjs-forum.com/post/1313889801510060124#message-1313890820046458901) and the client now set's it to a different value, then what happens: hydration error. So it wouldn't resolve your hydration error when you just set it.

Import your client component with disabled SSR and you are good to go:
const ComponentC = dynamic(() => import('../components/C'), { ssr: false })
Answer
Avatar
happy to help
Avatar
@B33fb0n3 happy to help
Avatar
NebelungOP
But what if i pass children to this component with SSR false? Will they also have ssr disabled for them?
Avatar
@B33fb0n3 that will also disabled them from beeing loaded as SSR
Avatar
NebelungOP
That's a problem :|
Avatar
@Nebelung That's a problem :|
Avatar
why?
Avatar
NebelungOP
This component is a provider that basically wraps my whole app :lolsob:
Avatar
@B33fb0n3 then set it to: <div onTouchStart=""/>
Avatar
NebelungOP
Well is there any way to really do this for now?
Or is it just not possible to produce such HTML on server with nextjs
Avatar
it is possible. I just tried to use it like I said and of course there is an typescript error, but the html renders fine. However: I think nextjs itself will strip all props from html element that are "invalid". So maybe the event itself won't be there.
What are you trying to do? You said it's a provider? Give us a little context and maybe I can give you some experience
Avatar
@B33fb0n3 What are you trying to do? You said it's a provider? Give us a little context and maybe I can give you some experience
Avatar
NebelungOP
I have a provider from this library wrapping my app (TonConnectUIProvider). It has in it's internal logic some usEffect that does this

https://www.npmjs.com/package/@tonconnect/ui-react
Image
So i have to either match what it does in my server rendered html, disable SSR for it and all of it's children (not considering that) or patch the library
Third option seems like the best one although i'm not sure if removing ontouchstart="" wouldn't break anything. And if it does we come right back to the problem that i have no idea how to set such an attribute to the body properly in nextjs.
Avatar
@B33fb0n3 you can set it by adding it directly to your element like: tsx <div onTouchStart={() => {}}/>
Avatar
NebelungOP
This is what i get when trying to pass it an empty function
Image
Avatar
hm ok. Can you extract only this single div into it's own component?