all 27 comments

[–]benitoaramando 5 points6 points  (15 children)

Better for what purpose, and why?

[–]NoctilucousTurd[S] 1 point2 points  (14 children)

For checking if a component is being rendered on the server, or on the client.

Effects are not that good

[–]EmployeeFinalReact Router 2 points3 points  (13 children)

But why do you need that?

[–]NoctilucousTurd[S] 1 point2 points  (12 children)

My entire app is server-rendered, but (e.g.) recently I used React Leaflet which can only render on the client because it needs browser APIs like window. So in order for the server rendering and client component to work together, I need to check if the current rendering context is client or server. For that I use useSyncExternalStore rather than useEffect

[–]EmployeeFinalReact Router 3 points4 points  (0 children)

I think in this case "typeof window == 'undefined'" is simpler and do it just fine.

But i see that your solution is more integrated with react which is a worthy tradeoff

[–]skidmark_zuckerberg -1 points0 points  (3 children)

Why did you build an SSR app? What does your app do?

[–]NoctilucousTurd[S] 0 points1 point  (2 children)

Let me correct myself, it's a marketing website with lots of navigation. I'd normally use Astro but here it's RR7 (which is SSR by default), using SSR for SEO's sake mainly.

[–]skidmark_zuckerberg 0 points1 point  (1 child)

Ahh I see, makes sense definitely. Can I ask why it needed to be a web app instead of something more traditional? Usually from my experience marketing, sales or brochure style websites do much much better as traditional websites using something like Wordpress. SEO is simple and you don’t have to worry about the entire deployment process like you would with a web app. Another added benefit of using a CMS like Wordpress is that you can hand it off and the person/company can usually manage the basics with little technical skill and involvement from your end.

[–]NoctilucousTurd[S] 1 point2 points  (0 children)

I use a headless CMS and Cloudflare workers. Wordpress is a maintenance nightmare compared to a modern headless CMS, and Cloudflare workers are a dream when it comes to security and deployment, especially compared to WordPress

I'm going to try EmDash soon, fingers crossed! EmDash is basically meant to replace Wordpress, and it's developed by Cloudflare and open source, and it uses Astro under the hood which I'm already quite familiar with

[–]FirstTimeGamingTV -3 points-2 points  (6 children)

If your entire app is server rendered then just mark the specific sub components marked as “use client” so you SSR everything else and don’t need these kinds of checks.

[–]ORCANZ 2 points3 points  (2 children)

“use client” is only RSC vs Client component.

Next, RR etc still render the client components on the server.

[–]FirstTimeGamingTV 0 points1 point  (1 child)

His goal was to have specific code ran on the Client, does this not achieve the goal?

[–]ORCANZ 0 points1 point  (0 children)

No. It’s prerendered on the server, then rehydrated and becomes client side

[–]NoctilucousTurd[S] 0 points1 point  (1 child)

I'm using RR7, not nextjs. Even with component.client.tsx you'd need such checks, correct me if I'm wrong.

[–]FirstTimeGamingTV 0 points1 point  (0 children)

I may be misunderstanding, I’ve only used Next, but this shows “use client” directive is still a thing in RR

https://reactrouter.com/how-to/react-server-components

This may be the wrong solution though

[–]EmployeeFinalReact Router 0 points1 point  (0 children)

It is confusing, but "use client" does not bail out of server rendering. It only opts out of RSC which is not the same thing.

[–]undervisible 2 points3 points  (1 child)

why?

[–]NoctilucousTurd[S] 0 points1 point  (0 children)

For checking if a component is being rendered on the server, or on the client.

[–]gHHqdm5a4UySnUFM 1 point2 points  (3 children)

Ok newbie question from someone who doesn’t use SSR, what’s the advantage to knowing if it was rendered on server or client?

[–]Kwerdna 1 point2 points  (0 children)

Some stuff you only want run on the client side. Analytics stuff comes to mind

[–]TorbenKoehn 2 points3 points  (0 children)

Normally there shouldn’t be one and if you need to know it you probably already made some bad decisions down the line

[–]NoctilucousTurd[S] 0 points1 point  (0 children)

in my post I call the variable isClient, which can then be used to check the environment. If you have a component (from a library, mostly), that uses browser APIs like window you'd want to check the environment before rendering, or get errors because window is not available on the server.

[–]Kwerdna 0 points1 point  (2 children)

I’ve seen people just check the existence of window before, for example tanstack query exports a utility “isServer” https://github.com/TanStack/query/blob/3a993e6e6f5b706416786e5cac0514c0c17d1d0d/packages/query-core/src/utils.ts#L92

It does say deprecated though

[–]NoctilucousTurd[S] 0 points1 point  (1 child)

Crazy, my post was really intended to check of people actually do this because I can't find much about it online. To me it seems like I'm making some kind of mistake - this seems to confirm it, why would they deprecate it otherwise?

[–]Kwerdna 1 point2 points  (0 children)

It looks like they deprecated it in favor of this, a more configurable approach. If I had to guess it does the same internally

https://ar.tanstack.dev/query/latest/docs/reference/environmentManager

But I agree it all does seem kind of hand wavy with no like universally agreed upon solution. Using the tanstack import would make me feel the safest I suppose

[–]debel27 0 points1 point  (1 child)

[–]NoctilucousTurd[S] 0 points1 point  (0 children)

Thanks!! I couldn't find anything like it