Install using bunx shadcn@latest add @remix-utils/use-should-hydrate.
Note
This depends on react-router and react.
If you are building a Remix application where most routes are static, and you want to avoid loading client-side JS, you can use this hook, plus some conventions, to detect if one or more active routes needs JS and only render the Scripts component in that case.
In your document component, you can call this hook to dynamically render the Scripts component if needed.
Now, you can export a handle object with the hydrate property as true in any route module.
exportlethandle = { hydrate:true };
This will mark the route as requiring JS hydration.
In some cases, a route may need JS based on the data the loader returned. For example, if you have a component to purchase a product, but only authenticated users can see it, you don't need JS until the user is authenticated. In that case, you can make hydrate be a function receiving your loader data.
Install using
bunx shadcn@latest add @remix-utils/use-should-hydrate.This depends on
react-routerandreact.If you are building a Remix application where most routes are static, and you want to avoid loading client-side JS, you can use this hook, plus some conventions, to detect if one or more active routes needs JS and only render the Scripts component in that case.
In your document component, you can call this hook to dynamically render the Scripts component if needed.
Now, you can export a
handleobject with thehydrateproperty astruein any route module.This will mark the route as requiring JS hydration.
In some cases, a route may need JS based on the data the loader returned. For example, if you have a component to purchase a product, but only authenticated users can see it, you don't need JS until the user is authenticated. In that case, you can make
hydratebe a function receiving your loader data.The
useShouldHydratehook will detecthydrateas a function and call it using the route data.Author
Sergio XalambrÃ