Categories
React Answers

How to fix div cannot appear as a descendant of p error with React?

Spread the love

To fix div cannot appear as a descendant of p error with React, we should make sure we don’t have divs that are inside p elements.

For instance, we shouldn’t write

<p>
  <div>...</div>
</p>

in our React components.

If we’re using the Material UI Typography component, we can change the component prop so that we don’t render a div inside a p element.

To do this, we write

<Typography component={"span"} variant={"body2"}>
  ...
</Typography>

to set the component prop to 'span' so that Typography doesn’t render a div inside a p element.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *