Giter Site home page Giter Site logo

Comments (7)

tylermcginnis avatar tylermcginnis commented on June 7, 2024

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.

mmkal avatar mmkal commented on June 7, 2024

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.

tylermcginnis avatar tylermcginnis commented on June 7, 2024

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.

mmkal avatar mmkal commented on June 7, 2024

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.

tylermcginnis avatar tylermcginnis commented on June 7, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.