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

Please update to React 18 #9

Open
gavin-c-tcg opened this issue Aug 23, 2022 · 2 comments
Open

Please update to React 18 #9

gavin-c-tcg opened this issue Aug 23, 2022 · 2 comments

Comments

@gavin-c-tcg
Copy link

Please update to React 18

@gavin-c-tcg
Copy link
Author

next js can use

import dynamic from "next/dynamic";

const SomeComponent = ()=>{
return <div />
}

const NoSSRSomeComponent  = dynamic(async () => SomeComponent, { ssr: false });

@douglascayers
Copy link

If you're using nextjs then @gavin-c-tcg's answer is spot-on.

Otherwise, here is a React 18 component with TypeScript people may use.

no-ssr.tsx

import { ReactNode, useEffect, useState } from 'react';

interface NoSSRProps {
  /**
   * Optional content to show before the component renders on client.
   * This renders during server-side rendering (SSR).
   */
  onSSR?: React.FC;
  /**
   * The content to render on client.
   */
  children?: ReactNode;
}

const EmptySpan: React.FC = (): ReactNode => <span />;

const NoSSR: React.FC<NoSSRProps> = (props: NoSSRProps): ReactNode => {
  const { onSSR = EmptySpan, children = <EmptySpan /> } = props;

  const [isMounted, setIsMounted] = useState<boolean>(false);

  useEffect(() => {
    setIsMounted(true);
    return () => {
      setIsMounted(false);
    };
  });

  return isMounted ? children : onSSR({});
};

export { NoSSR };

Usage

<NoSSR>
  <SomeComponent ... />
</NoSSR>

or

<NoSSR onSSR={() => <div>loading...</div>}>
  <SomeComponent ... />
</NoSSR>

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

2 participants