JSX and React Elements and DOM Elements

(JavaScript XML | React.createElement | DOM)

React Element (JS Object) JSX

React.createElement produces a JS object with all the necessary configurations to render it to the real DOM

Click for Demo in code sandbox

JSX is an Extension Syntax that allows writing HTML and Javascript together easily in React.

Each JSX element is a syntactic sugar for calling React.createElement()

return  React.createElement(
  'h1', null, 'Welcome dude!'
)
return <h1>Welcome dude!</h1>


Syntax for React.createElement

React.createElement( 
  component, props, ...children 
)
React.createElement( 
  elementName, 
  attribute, 
  ...children
)
  1. First parameter: The HTML element we want to create.
  2. Second parameter: HTML attributes or arbitrary inputs that are passed as parameters.
  3. Third parameter: Child Element

Syntax for JSX

<Component props />


<element attribute>
  ...children
</element>


From React Element to DOM tag

HTML Tag

If you provide a string, it will turn it into a DOM tag with that string as the value. Eg: We utilized the tags h1 and div, which are output to the DOM.

React Component

Function React.createElement creates a single instance of a component.

Elements

return React.createElement(
  "div",
  null,
  React.createElement(
    "h1",
    { className: "green" },
    `Welcome dude!`,
  ),
);

React.createElement inside React.createElement

Elements

return (
  <div>
    <h1 className="green">
        Welcome dude!
    </h1>
  </div>
)


Component

React.createElement works with both HTML elements and custom components

Below code Creates an instance of the Component

Component

JSX also works with both HTML elements and custom components

Below code Creates an instance of the Component

App.js

component instance in react createElement

App.js

component instance in react createElement

MyComponent.js

component in react createElement

MyComponent.js

component in react createElement

className attribute

Because class is a reserved keyword in JS, className is essential

What about props?

Also, we can pass our props to our components.

React Element (JS Object) JSX
React.createElement(
  Tag, 
  { name: 'Jhon', age: 31 }, 
  null
)
<Tag name="Jhon" age={31} />




Same vanilla JS has a createElement method, too: //document.createElement('div')

----------- Note: If you are a beginner - upto this is enough for now -----------



So, we've got these React elements that we made using React.createElement. But how does it rendered in the DOM?



Warnings:

createRoot Caveats

  • If your app is server-rendered, using createRoot() is not supported. Use hydrateRoot() instead.
  • You’ll likely have only one createRoot call in your app. If you use a framework, it might do this call for you.
  • When you want to render a piece of JSX in a different part of the DOM tree that isn’t a child of your component (for example, a modal or a tooltip), use createPortal instead of createRoot.

root.render(reactNode) Caveats

  • The first time you call root.render, React will clear all the existing HTML content inside the React root before rendering the React component into it.
  • If your root’s DOM node contains HTML generated by React on the server or during the build, use hydrateRoot() instead, which attaches the event handlers to the existing HTML.
  • If you call render on the same root more than once, React will update the DOM as necessary to reflect the latest JSX you passed. React will decide which parts of the DOM can be reused and which need to be recreated by “matching it up” with the previously rendered tree. Calling render on the same root again is similar to calling the set function on the root component: React avoids unnecessary DOM updates.

root.unmount() Caveats

  • Calling root.unmount will unmount all the components in the tree and “detach” React from the root DOM node.
  • Once you call root.unmount you cannot call root.render again on the same root. Attempting to call root.render on an unmounted root will throw a “Cannot update an unmounted root” error. However, you can create a new root for the same DOM node after the previous root for that node has been unmounted.

For more information Refer createRoot in React Docs