React Render HTML
π§± React Render HTML
React does not directly modify the browser DOM like traditional JavaScript. Instead, it renders HTML inside a root element using the ReactDOM library.
π The Root Element (HTML)
In a React project, open public/index.html and you’ll find this element:
React will render your entire application inside this element.
π Rendering HTML in React
React uses:
-
ReactDOM.createRoot() β creates root object
-
.render()β renders the component/UI into the DOM
Example:
π Using JSX to Render HTML
React doesnβt use plain HTML β it uses JSX (JavaScript XML), which looks like HTML but works inside JavaScript.
Example:
π― Rules of JSX (Important)
β Must return a single parent element
β Invalid:
β Valid:
OR using a fragment (<>...</>):
π§© Rendering Variables
JSX allows inserting JavaScript values using {}:
π Rendering Functions
π§± Rendering Component Instead of HTML
React usually renders components instead of plain HTML.
Example:
π Summary
React renders HTML using:
β root.render()
β JSX syntax
β One parent container
β Ability to embed JS values
β Components for reusable UI
