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: Warning: React.createElement: type is invalid #19

Open
seandawn opened this issue Nov 3, 2023 · 7 comments
Open

Error: Warning: React.createElement: type is invalid #19

seandawn opened this issue Nov 3, 2023 · 7 comments

Comments

@seandawn
Copy link

seandawn commented Nov 3, 2023

I have this component:

import { MuiMarkdown } from 'mui-markdown'
import guidelines from '../content-guidelines.md'
import React from 'react'

export default function ContentGuidelines() {
  return <MuiMarkdown options={{ wrapper: React.Fragment }}>{String(guidelines)}</MuiMarkdown>
}

The component renders, and whatever markdown I pass it displays correctly, however this appears in the error log:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at span (webpack-internal:///(ssr)/./node_modules/mui-markdown/dist/index.esm.js:21:5878)
at WEBPACK_DEFAULT_EXPORT (webpack-internal:///(ssr)/./node_modules/markdown-to-jsx/dist/index.module.js:7:13758)
at I (webpack-internal:///(ssr)/./node_modules/mui-markdown/dist/index.esm.js:21:7013)
at div
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at Grid (webpack-internal:///(ssr)/./node_modules/@mui/system/Stack/createStack.js:147:24)
at ContentGuidelinesPage
at StaticGenerationSearchParamsBailoutProvider (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/static-generation-searchparams-bailout-provider.js:15:11)

Versions:

"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/material": "^5.14.16",
"mui-markdown": "^1.1.11",

@HPouyanmehr
Copy link
Owner

Hey @seandawn,

This warning most of the time is related to improper imports/exports. In your case, it can related to the way you are importing the MD content. How did you set up your project to load markdown files? (As it is not supported by default, you can use @mdx-js/loader for example)

Please provide more information to make it easier to reproduce the issue.

@seandawn
Copy link
Author

seandawn commented Nov 3, 2023

Hi there, I've configured typescript to recognise md files as a string, and webpack to import it as source, which means it should just load the file contents, it's the new way instead of using raw-loader.

webpack: (config) => {
config.module.rules.push({
test: /.md$/,
type: 'asset/source',
})
return config
}

I'll try your way and report back.

@seandawn
Copy link
Author

seandawn commented Nov 3, 2023

Okay, not sure what's happening now.

I've set up MDX file handling as documented here - https://nextjs.org/docs/pages/building-your-application/configuring/mdx

I've created an MDX file and I can import a MUI button component and it renders in my MDX component fine, but as soon as I wrap the MD text in I get this error:

Unhandled Runtime Error
TypeError: t.replace is not a function

Call Stack
eval
node_modules\markdown-to-jsx\dist\index.modern.js (1:12198)

This is my MDX file:

import { MuiMarkdown } from 'mui-markdown'

<MuiMarkdown>
EXAMPLE MD TEXT
</MuiMarkdown>

@HPouyanmehr
Copy link
Owner

Thanks for your comments @seandawn,

I need to find some free time to check and reproduce this issue. Also, can you tell whether you're using nextjs 13 or 14?

@seandawn
Copy link
Author

seandawn commented Nov 4, 2023

I am using nextjs 13.5.6

I have a page set up like this:

import { Metadata } from 'next'
import { Stack, Typography } from '@mui/material'
import ContentGuidelines from './cmps/ContentGuidelines'

export const metadata: Metadata = {
  title: 'Content guidelines',
}

export default function ContentGuidelinesPage() {
  return (
    <Stack spacing={2}>
      <Typography variant='h1' textAlign={'center'}>
        Content guidelines
      </Typography>
      <ContentGuidelines />
    </Stack>
  )
}

As MuiMarkdown uses useStyles I created a separate client component called ContentGuidelines like so:

'use client'

import GuidelinesMDX from '../content-guidelines.mdx'

export default function ContentGuidelines() {
  return <GuidelinesMDX />
}

I then have this mdx file, I've removed the MD text as its sensitive:

import { MuiMarkdown } from 'mui-markdown'

<MuiMarkdown>
{`MD TEXT HERE`}
</MuiMarkdown>

Putting the MD text into the {``} braces has removed the TypeError: t.replace is not a function error, but now it is displaying this again:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at p (webpack-internal:///(ssr)/./node_modules/mui-markdown/dist/index.esm.js:21:5620)
at div
at WEBPACK_DEFAULT_EXPORT (webpack-internal:///(ssr)/./node_modules/markdown-to-jsx/dist/index.module.js:7:13758)
at I (webpack-internal:///(ssr)/./node_modules/mui-markdown/dist/index.esm.js:21:7013)
at MDXContent (webpack-internal:///(ssr)/./app/content-guidelines/content-guidelines.mdx:44:89)
at ContentGuidelines
at Lazy
at div
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at Lazy
at InnerLayoutRouter (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:240:11)
at RedirectErrorBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/redirect-boundary.js:71:9)
at RedirectBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/redirect-boundary.js:79:11)
at NotFoundBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/not-found-boundary.js:62:11)
at LoadingBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:336:11)
at ErrorBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/error-boundary.js:109:11)
at InnerScrollAndFocusHandler (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:152:9)
at ScrollAndFocusHandler (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:227:11)
at RenderFromTemplateContext (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/render-from-template-context.js:15:44)
at Lazy
at OuterLayoutRouter (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:345:11)
at Lazy
at div
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at Grid (webpack-internal:///(ssr)/./node_modules/@mui/system/Unstable_Grid/createGrid.js:63:41)
at Lazy
at div
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at Grid (webpack-internal:///(ssr)/./node_modules/@mui/system/Unstable_Grid/createGrid.js:63:41)
at Lazy
at InnerLayoutRouter (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:240:11)
at RedirectErrorBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/redirect-boundary.js:71:9)
at RedirectBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/redirect-boundary.js:79:11)
at NotFoundErrorBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/not-found-boundary.js:54:9)
at NotFoundBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/not-found-boundary.js:62:11)
at LoadingBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:336:11)
at ErrorBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/error-boundary.js:109:11)
at InnerScrollAndFocusHandler (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:152:9)
at ScrollAndFocusHandler (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:227:11)
at RenderFromTemplateContext (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/render-from-template-context.js:15:44)
at Lazy
at OuterLayoutRouter (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/layout-router.js:345:11)
at Lazy
at main
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at Box (webpack-internal:///(ssr)/./node_modules/@mui/system/createBox.js:31:41)
at Lazy
at SnackbarProvider (webpack-internal:///(ssr)/./app/SnackbarProvider.tsx:28:29)
at Lazy
at body
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at Lazy
at TipsProvider (webpack-internal:///(ssr)/./app/TipsProvider.tsx:27:25)
at Lazy
at LocalizationProvider (webpack-internal:///(ssr)/./node_modules/@mui/x-date-pickers/node/LocalizationProvider/LocalizationProvider.js:35:19)
at LocalizationProvider (webpack-internal:///(ssr)/./app/LocalizationProvider.tsx:27:33)
at Lazy
at SocketProvider (webpack-internal:///(ssr)/./app/WebsocketProvider.tsx:29:27)
at Lazy
at SessionProvider (webpack-internal:///(ssr)/./node_modules/next-auth/react/index.js:454:24)
at SessionProvider (webpack-internal:///(ssr)/./app/SessionProvider.tsx:11:28)
at Lazy
at html
at eval (webpack-internal:///(ssr)/./node_modules/@emotion/react/dist/emotion-element-6bdfffb2.esm.js:68:68)
at Box (webpack-internal:///(ssr)/./node_modules/@mui/system/createBox.js:31:41)
at Lazy
at ThemeProvider (webpack-internal:///(ssr)/./node_modules/@mui/private-theming/node/ThemeProvider/ThemeProvider.js:39:5)
at ThemeProvider (webpack-internal:///(ssr)/./node_modules/@mui/system/ThemeProvider/ThemeProvider.js:50:5)
at ThemeProvider (webpack-internal:///(ssr)/./node_modules/@mui/material/node/styles/ThemeProvider.js:21:14)
at ThemeProvider (webpack-internal:///(ssr)/./app/ThemeProvider.tsx:16:26)
at Lazy
at RedirectErrorBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/redirect-boundary.js:71:9)
at RedirectBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/redirect-boundary.js:79:11)
at ReactDevOverlay (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/react-dev-overlay/internal/ReactDevOverlay.js:66:9)
at HotReload (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/react-dev-overlay/hot-reloader-client.js:295:11)
at Router (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/app-router.js:154:11)
at ErrorBoundaryHandler (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/error-boundary.js:82:9)
at ErrorBoundary (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/error-boundary.js:109:11)
at AppRouter (webpack-internal:///(ssr)/./node_modules/next/dist/client/components/app-router.js:426:13)
at Lazy
at Lazy
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:420630
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:420630
at ServerInsertedHTMLProvider (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:36:27168)

⨯ Internal error: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:43931)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at aR (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:52522)
at aC (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:50400)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47657)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:40018)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at ab (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:8795)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13910)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at ab (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:8795)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13910)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at aR (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:52522)
at aC (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:50400)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47657)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at aR (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:52522)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:39403
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:39881)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at ab (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:8795)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13910)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at ab (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:8795)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13910)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at ab (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:8795)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13910)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)
at ab (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:8795)
at C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13886
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:13910)
at aS (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:43674)
at e (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:47308)
at a_ (C:\Users\vacuu\websites\submit\node_modules\next\dist\compiled\next-server\app-page-experimental.runtime.dev.js:34:49286)

It does render the markdown correctly, but ideally I don't want my error log filling up with these warnings and errors, if you need anything else let me know.

@gersur
Copy link

gersur commented Nov 24, 2023

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
...

I got the same error with Next.js 14.
If you look closely, Next.js doesn't generate SSG/SSR when mui-markdown is used. The browser skips to CSR

---EDIT
markdown-to-jsx works out-of-the-box in SSR

--EDIT 2

// NOT WORKING DO NOT COPY
"use client";

import MarkdownRoot from "markdown-to-jsx";
import { getOverrides } from "mui-markdown";

interface Props {
  children: string;
}

const _Markdown: React.FC<Props> = ({ children, ...props }) => {
  return (
    <MarkdownRoot
      options={{
        overrides: getOverrides(),
      }}
    >
      {children}
    </MarkdownRoot>
  );
};

This code doesn't work :(. So the problem is from mui-library.
I think its the styled component from @mui doesn't work well with SSR

--EDIT 3

As of late 2021, styled-components is not compatible with >? server-rendered Material UI projects. This is because babel-plugin-styled-components isn't able to work with the styled() utility inside @mui packages. See this GitHub issue for more details.

We strongly recommend using Emotion for SSR projects.
https://mui.com/material-ui/guides/styled-components/

--EDIT 4

Workaround

SSR markdown-to-jsx and CSR mui-markdown, but there will be FOUC

@/components/Markdown/Markdown.tsx

"use client";

import { MuiMarkdownProps } from "mui-markdown";
import dynamic from "next/dynamic";
import React, { useEffect, useState } from "react";

const MuiMarkdown = dynamic(() => import("mui-markdown").then((x) => x.MuiMarkdown), { ssr: false });

type Props = Omit<MuiMarkdownProps, "children"> & { source: string } & React.PropsWithChildren;

const _Markdown: React.FC<Props> = ({ source, children, ...props }) => {
  const muiMarkdown = <MuiMarkdown {...(props as any)}>{source}</MuiMarkdown>;

  const [component, setComponent] = useState(children);

  useEffect(() => {
    setComponent(muiMarkdown);
  }, []);

  return component;
};

export const Markdown = _Markdown;

page.tsx

import { Markdown } from "@/components/Markdown/Markdown";
import MarkdownRoot from "markdown-to-jsx";

<Markdown source={"_markdown string here"}>
  <MarkdownRoot>{"_markdown string here"}</MarkdownRoot>
</Markdown>

@trevorr
Copy link

trevorr commented Mar 16, 2024

I was hitting this with Next.js 13/14 as well. The cause was the MUI components not being imported correctly. When I dumped the object being passed to create element (Typography in the case I was debugging), it had a default getter to the component instead of being the component itself. The fix for me was to remove transpilePackages: ["mui-markdown"] from next.config.js. I had to add this previously in Next 13, but both dev and production seem to work without it in Next 14. (I had upgraded to Next 14 hoping it would solve the problem, which it didn't, at least not by itself.)

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

4 participants