Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation on how to use client-hooks and improve errors #259

Open
ottovw opened this issue Nov 2, 2023 · 2 comments
Open

Add documentation on how to use client-hooks and improve errors #259

ottovw opened this issue Nov 2, 2023 · 2 comments

Comments

@ottovw
Copy link

ottovw commented Nov 2, 2023

After running into a issue using client hooks and server rendering, @tylermcginnis referenced/explained (thanks!) why that error is necessary: #218 (comment)

It turns out, that happened to multiple users:

Therefore the proposal is to:

  • add a structured docs block to every client hook in the docs (e.g. using the explanation of Tyler) on the website
  • include the url to this explanation within the error message in the code
  • mark client-hooks in the hook overview (maybe also sort?)

useHooks advertises "server-safe" which is a bit misleading. I acknowledge, that it was a user error due to not fully understanding server render. useHooks could make life much easier for the users. My initial experience was pretty frustrating. (Remember Apple's "You're holding it wrong")

Thanks for providing this library.

@jamesvclements
Copy link

Here's a utility component I wrote for client-only hooks:

"use client";

import { useIsClient } from "@uidotdev/usehooks";

const ClientOnly = ({
  children,
  placeholder,
}: {
  children: React.ReactNode;
  placeholder?: React.ReactNode;
}) => {
  const isClient = useIsClient();

  if (!isClient) {
    return placeholder || null;
  }

  return children;
};

export default ClientOnly;

Usage:

<ClientOnly
  placeholder={<div className="fixed inset-0 bg-black z-50" />}
>
  <Preloader video={home.video as VideoType} />
</ClientOnly>

@wayneschuller
Copy link

I support this suggestion.

What I find strange, if some of these hooks can ONLY really make sense on clients, why require the useIsClient hook or other wrappers?

Could not useLocalStorage hook incorporate isClient tests internally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants