(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() |
|
|
Syntax for React.createElementReact.createElement( component, props, ...children ) React.createElement( elementName, attribute, ...children )
|
Syntax for JSX<Component props /> <element attribute> ...children </element> |
From React Element to DOM tagHTML TagIf 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 ComponentFunction React.createElement creates a single instance of a component. |
|
Elements
React.createElement inside React.createElement |
Elements
|
ComponentReact.createElement works with both HTML elements and custom components Below code Creates an instance of the Component |
ComponentJSX also works with both HTML elements and custom components Below code Creates an instance of the Component |
App.js ![]() |
App.js ![]() |
MyComponent.js ![]() |
MyComponent.js ![]() |
Because class is a reserved keyword in JS, className is essential
Also, we can pass our props to our components.
React Element (JS Object) | JSX |
---|---|
|
|
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?
createRoot lets you create a root to display React components inside a browser DOM node.
const root = ReactDOM.createRoot(document.getElementById('root'));
createRoot returns an object with two methods: render and unmount.
After you’ve created a root, you need to call .render() to display a React component inside of it:
React Element (JS Object) | JSX |
---|---|
|
|
render
renders a piece of JSX (“React node”) into a browser DOM node.
To render out, we'll need an instance of App. The app is a component Object, and we only need to render one instance of it. JSX -> React.createElement accomplishes exactly that: it creates an Object instance.
Below example - Displays your React Component inside the HTML element containing the appropriate id
takes our rendered App component (present in './index.js') and places it in the DOM (in our example, the root element - present in './public/index.html').
Call root.unmount
to destroy a rendered tree inside a React root.
root.unmount();
Calling root.unmount
will unmount all the components in the root and “detach” React from the root DOM node, including removing any event handlers or state in the tree.
createRoot()
is not supported. Use hydrateRoot()
instead.
root.render
, React will clear all the existing HTML content inside the React root before rendering the React component into it.
hydrateRoot()
instead, which attaches the event handlers to the existing HTML.
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
will unmount all the components in the tree and “detach” React from the root DOM node.
For more information Refer createRoot in React Docs