<div> cannot appear as a descendant of <p>
July 26th 2024

unPossible Ways to Fix It

The following strategies can help address this error:

Solution 1: Using useEffect to run on the client only

Ensure that the component renders the same content server-side as it does during the initial client-side render to prevent a hydration mismatch. You can intentionally render different content on the client with the useEffect hook.

import { useState, useEffect } from 'react'
 
export default function App() {
  const [isClient, setIsClient] = useState(false)
 
  useEffect(() => {
    setIsClient(true)
  }, [])
 
  return <h1>{isClient ? 'This is never prerendered' : 'Prerendered'}</h1>
}

 

During React hydration, useEffect is called. This means browser APIs like window are available to use without hydration mismatches.

Solution2: Disabling SSR on specific components

Next.js allows you to disable prerendering on specific components, which can prevent hydration mismatches.

import dynamic from 'next/dynamic'
 
const NoSSR = dynamic(() => import('../components/no-ssr'), { ssr: false })
 
export default function Page() {
  return (
    <div>
      <NoSSR />
    </div>
  )
}

Solution 3: Using suppressHydrationWarning

Sometimes content will inevitably differ between the server and client, such as a timestamp. You can silence the hydration mismatch warning by adding suppressHydrationWarning={true} to the element.

Solution 4: 

If this error occurs while using Material UI <Typography> https://material-ui.com/api/typography/, then you can easily change the <p> to a <span> by changing the value of the component attribute of the <Typography> element :

 

<Typography variant="body1" paragraph component={'span'}>
                            <div suppressHydrationWarning className="article ck-content" dangerouslySetInnerHTML={{ __html: blog.description }} />
                        </Typography>

According to the typography docs:

component : The component used for the root node. Either a string to use a DOM element or a component. By default, it maps the variant to a good default headline component.

So Typography is picking <p> as a sensible default, which you can change. May come with side effects ... worked for me.

 

 

 

Error displaying when running yarn build: ""build": "NODE_ENV=production next build"

 

Error occurred prerendering page "/admin/blog/add". Read more: https://nextjs.org/docs/messages/prerender-error

TypeError: e.createElement is not a function

 refer Solution2 above:  Disabling SSR on specific components
 

The error TypeError: e.createElement is not a function during the build process in a Next.js application often occurs when there’s an issue with how React components or elements are being imported or used in your code. This is especially common when dealing with third-party libraries or custom components.
Ensure Compatibility with Server-Side Rendering (SSR)

 

Some libraries or components might not be compatible with SSR, which is used by Next.js during the build process. If you suspect this might be the case, try to dynamically import the component with ssr: false.

import dynamic from 'next/dynamic';

const MyComponent = dynamic(() => import('../../components/MyComponent'), { ssr: false });

export default function AddBlogPage() {
  return <MyComponent />;
}

OR if you using a custom Editor such as CKEditor 5

const CustomEditor = dynamic(() => import('@/app/(sidemenu)/storage/CustomEditor'), {
  ssr: false,
})
Huy Le
Huy Le
[email protected]

Full-stack developer passionate about React and react native, firebase

Tags
React
Next
mui
mui framework
Social Share
Loading...