Comments (7)
Believe it or not, "use client" doesn't mean it only runs on the client 🙃
This should point you in the right direction - #218
from usehooks.
Thanks, missed all those other issues. Good to know there's a workaround.
Still, seems like a usability problem. I know client components also have an SSR pass, but the important thing is they will run in the client, so shouldn't it be fine to return the initial value when pre-rendering?
from usehooks.
No, because if the value in localStorage when React hydrates is different than the default value you established on the server, you'll get a client/server mismatch error.
from usehooks.
I see. I guess I'd assumed that an additional render would take care of that on the client side. I just read through the useSyncExternalStore
docs and it says re getServerSnapshot
:
If you omit this argument, rendering the component on the server will throw an error
So maybe this library doesn't need to explicitly throw anyway? Is there any way you can think of that would allow for developers to have a server-default behaviour without wrapping the whole component (e.g. if you do useLocalStorage('k', 'v', { ssr: true })
then it always uses initialValue
on first render, then updates in the next render)? Maybe this library could bake in any or all of the three suggestions on the page you linked? https://nextjs.org/docs/messages/react-hydration-error, or maybe a suspense/error boundary that can catch the prerender error and show a fallback UI, higher up in the tree?
from usehooks.
It doesn't need to throw an error, but (IRRC) the one we throw is more descriptive than the one React does. I think the solution is to make the docs more clear that it should be used together with useIsClient
when you're working in a Next app. Been meaning to do that but haven't yet. Don't love the idea of trying to get around hydration errors.
from usehooks.
Related Issues (20)
- Can't stop propagation with useClickAway HOT 3
- Type 'MutableRefObject<Element>' is not assignable to type 'LegacyRef<HTMLDialogElement> | undefined'. HOT 1
- useScript should return loaded when script is already in the page HOT 6
- useLocalStorage only allows strings that can be JSON.parse'd HOT 2
- FR: useEffectOnce() and useUpdateEffect() HOT 1
- wrong text is copied HOT 3
- useMediaQuery causes error when used with Nextjs (not server safe) HOT 2
- Add documentation on how to use client-hooks and improve errors HOT 2
- Request: Bring back code snippets HOT 1
- 🪲 BUG: Can't use the hooks on contextAPI file in NextJs14 HOT 1
- Where is the source code? HOT 1
- useHover has incorrect state when the component is mounted below the cursor
- Error when running react-script test with jest HOT 3
- jest test arent passing HOT 1
- ERROR: Cannot assign to import "useEffectEvent" when importing useKeyPress hook HOT 5
- useMediaQuery not working on iPhone 6 with iOS 12.5.7 HOT 1
- onLongPress : movement during press
- useLocalStorage is a client-only hook error on next.js project HOT 2
- Add new hook - useCookieStorage
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 usehooks.