Why the Div in React ?
Tags: javascript, react
Published: 2020-11-05 20:38:59

So when you first start using react, you will have no doubt written something like the below 

```Javascript
const App = () => {
    return(
      <p>Hello</p>
      <p>World</p>
     )
}
```

What's wrong with that you might ask ?

React tries to convert our JSX and this pops out
```Javascript
 Failed to compile.
 ./src/App.js
Syntax error: Adjacent JSX elements must be wrapped in
an enclosing tag (6:8) 
```
﻿
Behind the scenes for all the JSX statement you write in a component, they need to be wrapped into one container. But wait why is it necessary ? 

To answer this we have to think about what happens when React turns our JSX into eventually the HTML we see on the page. The first step in this process is to convert any JSX statement into an object. Behind the scenes React takes our JSX and a transpiler like Babel feeds the parts of that JSX into the `React.createElement` function.

If we look at the API for createElement

`function createElement(elementType, props, […children]) {}`

The arguments are defined as

1) `elementType` - The HTML Tag you want to display

2) `props` - Data like attributes or styling you want to pass

3) `children` - Anything you want to pass between the eventual HTML tags, most likely some text but can be other things (see below!)

Here's the example of our JSX from above 
```Javascript
<p>Hello</p> // JSX

```
Becomes
```
React.createElement(<p>, null, 'Hello')
```
The `React.createElement` function takes only one 'elementType' , that is the <p> part of our JSX element we want to eventually display. 

We can't have two JSX statements that will individually have separate `React.createElement` functions. This means two return statements and React will throw an error! Each function must only return one value in JavaScript.

So what is the solution to this conundrum? 

We wrap our JSX into a div. 
```Javascript
const App = () => {
    return( 
       <div>
         <p>Hello</p>
         <p>World</p>
      </div>
     )
  }

```

So behind the scenes this is what it looks like
```Javascript
   Return (
     React.createElement('div', 'null',       
        [React.createElement('p','null','Hello'),
        React.createElement('p','null','Hello')]
        )
     )
```
The App function can only return one value, by making our JSX statement a child of a div we can assign as many `React.createElement`'s as we like to get the output what we want.

Now the problem here with wrapping inside a div is that we end up bloating the DOM up with pointless div sitting in our eventual HTML. This may not be a problem for a simple component, but more complex components you can imagine how this affects the eventual running of the component and App. 

The other problem you may come into contact with is layout in your React application. You can imagine if there are div elements where there shouldn't be, using FlexBox to layout your page may not look the way you intended.

To deal with this problem, we will see in the next article!

##About the Author 

I'm a practising medical physician and educationalist as well as a web developer.

Please see <a href="http://www.coding-medic.com">here</a> for further details about what I'm up to project-wise on my blog and other posts.

I'd be grateful for any comments or if you want to collaborate or need help with python please do get in touch. If you want to get in contact with me, please do so here 
aaron.smith.07@aberdeen.ac.uk