Elements are the smallest building blocks of React apps.
An element describes what you want to see on the screen
Before diving in to see how these are created in React, let’s take a look at how DOM elements are generally created using plain JavaScript:
// First, we create the elementconstelement=document.createElement('button');element.setAttribute('type','submit');element.innerHTML='Click me!';// Then, we append it to the DOMdocument.body.appendChild(element);
React.createElement()
Creating elements in React has many similarities to creating elements in the DOM. We need to specify the type of element we want to create, give it certain parameters (called props in React), and the children that should be rendered within our element.
Notice how the HTMLclassattribute has to be set via theclassNameproperty in React. This is becauseclassis a reserved keyword in JavaScript.
Note that React elements are a lightweight representation of the DOM, and are not actual DOM elements. They are represented as plain objects and are cheap to create.
ReactDOM
React is split into two different packages, react and react-dom. The react-dom package provides DOM-specific methods that can be used at the top level of your app to render React elements into DOM elements.
ReactDOM.render()
The ReactDOM.render() method is used to render React Elements to the DOM, and takes two arguments:
The ReactElement to render
The DOM node we want to render into (the “entry point”)