Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does React strangely render the <p> (paragraph) tag?

Tags:

reactjs

React is doing strange things with the <p> tag. Using the same markup structure, with a <p> tag vs a <div> tag produces very different results. For example,

var withP = (
    <p>
      withP
      <div />
    </p>
);

var withDiv = (
    <div>
      withDiv
      <div />
    </div>
);

Here is what the generated markup looks like in chrome:

screenshot from chrome dev tools

Here is a live jsbin demo.

Why does React render <p> differently than <div>?

like image 993
Gil Birman Avatar asked Apr 14 '15 21:04

Gil Birman


People also ask

Can P be inside div?

Most elements that are used in the body of documents and applications are categorized as flow content. That includes p elements, which can only be used where flow content is expected. Therefore, div elements can contain p elements.

What is render () in React and why is it used?

React renders HTML to the web page by using a function called render(). The purpose of the function is to display the specified HTML code inside the specified HTML element. In the render() method, we can read props and state and return our JSX code to the root component of our app.

How do you render rich text in React?

Render Rich Text In your new React application, open slices/Text/index. js. This is the React component for your Text Slices. Your text is rendered by the <PrismicRichText> component.


1 Answers

<p> can not have nested block elements. Chrome (not React) is transforming the markup to make it valid.

like image 187
Gil Birman Avatar answered Oct 19 '22 20:10

Gil Birman