Comments (7)
You can call a hook anywhere you want in a compnent as long as it's not called conditionally. I put it in stringify here just for the simple example, most folks would call it at the top of the function so it's more obvious:
function NetworkState() {
let network = useNetworkState()
return <pre>{JSON.stringify(network)}</pre>;
}
from remix.
I am experiencing a similar error, but I am using the "module" folder approach. The module has several folders, each with its own exported index.ts file. Before Vite, I was able to import everything from '~/modules/foo', but now I can't.
Is there a way to solve this "issue" without creating a separate folder called .server
?
franklinjavier/remix-starter-template#75
from remix.
From a quick glance, it looks like this library uses useSyncExternalStore
to detect "server" which also means that it will detect "server" during the initial hydration render on the client - which makes sense to avoid hydration issues. That also explains why you still see the error when moving it into a .client
folder. That avoids the issue during SSR but still tries to use the hook during the initial hydration render and the useSyncExternalStore
snapshot triggers the error.
Error: useNetworkState is a client-only hook
at getNetworkStateServerSnapshot
at Object.useSyncExternalStore
...
Instead, you want to avoid running this hook until after hydration - which you can do with the remix-utils ClientOnly component:
import { useNetworkState } from "@uidotdev/usehooks";
import { ClientOnly } from "remix-utils/client-only";
export default function Index() {
return (
<div>
<NetworkState />
<ClientOnly>{() => <NetworkState />}</ClientOnly>
</div>
);
}
function NetworkState() {
return <pre>{JSON.stringify(useNetworkState())}</pre>;
}
from remix.
@brophdawg11 should I open a new issue or discussion about my doubt?
from remix.
I would see if the vite-env-only
solution solves it via https://remix.run/docs/en/main/future/vite#splitting-up-client-and-server-code - and if not, yeah, please open a new issue for that since it's not related to this specific hook use-case.
from remix.
@brophdawg11 how would you use hook in this case? Can you elaborate before closing?
would you add let network = useNetworkState() inside stringify
from remix.
Thanks @brophdawg11
I'll add this for future references
// using with context
import { createContext } from 'react'
import { ClientOnly } from 'react-utils/client-only' <-- v7.0.0-pre.7
import { useClientHook } from 'lib-with-probs'
const Context = createContext({})
function ForContext({ children }) {
const foo = useClientHook()
// To test I just added divs
return (
<div>
{foo ? <div>its on</div> : <div>its off</div>}
{children}
</div>
)
}
export const ContextProvider = ({ children }) => (
<Context.Provider>
<ClientOnly fallback={children}>
{() => <ForContext>{children}</ForContext>}
</ClientOnly>
<Context.Provider>
)
from remix.
Related Issues (20)
- Splat Routes that would have been prioritized can be overridden by Dynamic Segments. HOT 7
- remix hydration error when using gtag
- [Vite] [Cloudflare] dev server start failed with http_proxy environment variable
- Onclick handler not working in remix and problem installing tailwindcss HOT 5
- MetaFunction data type not inferred from clientLoader HOT 3
- Application Error: failed to execute insertBefore on Node HOT 19
- Vitest looks for package.json in every workspace when using vitePlugin HOT 2
- [BUG]: cloudflareDevProxyVitePlugin: Issue in DEV with Remix/Vite/Drizzle/Cloudflare Pages/Supabase/Postgres HOT 2
- Can't import node_module SVG HOT 2
- Streaming does work not on Vite/MacOS HOT 2
- Hydration issues in prod builds of Cloudflare starter template HOT 3
- remix spa mode cannot with `[email protected]` , when run `npm run preivew`, it's a empty page HOT 4
- Express Vite template loads white screen in production mode HOT 10
- Basic Routing Works on Server but not Client HOT 4
- Firestore Lite Does Not Work in Loader HOT 5
- Stdio pipe impacts plugin shutdown
- TypeScript: useLoaderData is not working properly in 2.9 HOT 10
- Unexpected Server Error TypeError: Cannot read properties of null (reading 'useEffect') HOT 1
- Hydration Errors When Deferred Promise Rejects HOT 1
- useFetcher return type is `any` since upgrading to remix 2.9.0 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from remix.