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

Error in Documentation for _document.tsx #441

Closed
anoushk1234 opened this issue Apr 29, 2022 · 7 comments · Fixed by #452
Closed

Error in Documentation for _document.tsx #441

anoushk1234 opened this issue Apr 29, 2022 · 7 comments · Fixed by #452
Labels
🐛 Type: Bug Something isn't working

Comments

@anoushk1234
Copy link
Contributor

Describe the bug

heres the code from the docs

import Document, {
  Html,
  Head,
  Main,
  NextScript,
  DocumentContext,
  DocumentInitialProps,
} from "next/document";
import { CssBaseline } from "@nextui-org/react";
import { ReactElement } from "react";

class MyDocument extends Document {
  static async getInitialProps(
    ctx: DocumentContext
  ): Promise<DocumentInitialProps> {
    const initialProps = await Document.getInitialProps(ctx);
    return {
      ...initialProps,
      styles: <>{initialProps.styles}</>,
    };
  }

  render() {
    return (
      <Html lang="en">
        <Head>{CssBaseline.flush()}</Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

i get a type error at this line styles: <>{initialProps.styles}</>, saying Type 'Element' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>>[] | ReactFragment | undefined'

if i remove the <> </> it doesnt show a type error and it works

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

Follow the nextjs guide in the docs https://nextui.org/docs/guide/getting-started#nextjs

Expected behavior

the documentation should have the correct code

Screenshots or Videos

No response

Operating System Version

macOS

Browser

Firefox

@anoushk1234 anoushk1234 added the 🐛 Type: Bug Something isn't working label Apr 29, 2022
@tianenpang
Copy link
Member

tianenpang commented Apr 29, 2022

Hey @anoushk1234 I don't think it's a issue with NextUI.

My solution is to use React.Children.toArray I'm not sure if there's a better way.

Reference: vercel/next.js#36008 (comment)

Example: https://codesandbox.io/s/next-ui-ts-bf0t1z

import { Children } from "react";
import { CssBaseline } from "@nextui-org/react";
import Document, { Html, Main, Head, NextScript } from "next/document";

const MyDocument = () => {
  return (
    <Html>
      <Head>{CssBaseline.flush()}</Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
};


MyDocument.getInitialProps = async (context) => {
  const initialProps = await Document.getInitialProps(context);

  return {
    ...initialProps,
    styles: [ ...Children.toArray(initialProps.styles) ]
  };
};

@anoushk1234
Copy link
Contributor Author

Hey @anoushk1234 I don't think it's a issue with NextUI.

My solution is to use React.Children.toArray I'm not sure if there's a better way.

Reference: vercel/next.js#36008 (comment)

Example: https://codesandbox.io/s/next-ui-ts-bf0t1z

import { Children } from "react";
import { CssBaseline } from "@nextui-org/react";
import Document, { Html, Main, Head, NextScript } from "next/document";

const MyDocument = () => {
  return (
    <Html>
      <Head>{CssBaseline.flush()}</Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
};


MyDocument.getInitialProps = async (context) => {
  const initialProps = await Document.getInitialProps(context);

  return {
    ...initialProps,
    styles: [ ...Children.toArray(initialProps.styles) ]
  };
};

hey, if it's not an issue with the defined types then the documentation needs to be update because styles: <>{initialProps.styles}</>, doesn't work

@jrgarciadev
Copy link
Member

Hey @anoushk1234 could you please share with us your tsconfig.json?

@anoushk1234
Copy link
Contributor Author

Hey @anoushk1234 could you please share with us your tsconfig.json?

Yes sure, here you go https://github.com/anoushk1234/anoushk.xyz/blob/main/tsconfig.json

@NayamAmarshe
Copy link

NayamAmarshe commented May 4, 2022

Here's how I fixed it:

import Document, { Html, Head, Main, NextScript } from "next/document";
import { CssBaseline } from "@nextui-org/react";

class MyDocument extends Document {
  static async getInitialProps(ctx: any) {
    const initialProps = await Document.getInitialProps(ctx);
    return {
      ...initialProps,
      styles: [<>{initialProps.styles}</>],
    };
  }

  render() {
    return (
      <Html lang="en">
        <Head>{CssBaseline.flush()}</Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

@anoushk1234
Copy link
Contributor Author

Here's how I fixed it:

import Document, { Html, Head, Main, NextScript } from "next/document";
import { CssBaseline } from "@nextui-org/react";

class MyDocument extends Document {
  static async getInitialProps(ctx: any) {
    const initialProps = await Document.getInitialProps(ctx);
    return {
      ...initialProps,
      styles: [<>{initialProps.styles}</>],
    };
  }

  render() {
    return (
      <Html lang="en">
        <Head>{CssBaseline.flush()}</Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

ohk got it, I'll update the docs with this code

@marcoripa96
Copy link

This doesn't work for me. React throws a warning for duplicate keys when using an array. I'm ignore ts typechecking at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants