October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Convert HTML to a React Component

Turn an HTML snippet into a React component with the required JSX changes, then render it safely in a new or existing page.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert HTML to a React component, place the markup inside a JavaScript function that returns JSX, then update the markup for JSX’s stricter syntax. The key fixes are to wrap adjacent top-level elements, use className instead of class, and explicitly close elements such as <img />. A converter can help with a large snippet, but you still need to review its output and choose how the component fits into your app.

Convert the HTML markup to JSX

JSX looks like HTML, but it is written in JavaScript and follows stricter rules. React recommends using a converter to translate existing HTML or SVG to JSX, then checking the result. See React’s Writing Markup with JSX guide.

For example, this HTML:

<h1 class="title">Welcome</h1>
<img src="/welcome.png" alt="Welcome">

can become a component like this:

export default function Welcome() {
  return (
    <>
      <h1 className="title">Welcome</h1>
      <img src="/welcome.png" alt="Welcome" />
    </>
  );
}

The changes are JSX syntax adjustments; this example does not add dynamic behavior.

Make the markup valid JSX

  • Wrap sibling top-level elements. A component must return one enclosing element. Use a meaningful element such as <main> when appropriate, or a fragment (<>...</>) when you do not want an extra DOM element.
  • Close every element. JSX requires explicit closing tags. Void elements such as <img> and <br> should be self-closing, for example <img />.
  • Use JSX property names. Change class to className and use camelCase for many DOM properties, such as tabIndex. Hyphenated aria-* and data-* attributes remain hyphenated.

These rules and other JSX examples are covered in React’s JSX guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put the JSX in a component

A React component is a function that returns UI. Save the function in a JavaScript module used by your React project, and import it wherever that UI belongs. React’s Describing the UI guide explains components and how they can be composed.

Conversion changes markup syntax; it does not decide which parts should be separate reusable components, replace fixed content with props, or implement interactions. Make those changes when the UI needs them: for example, a repeated card can become its own component, and a title that varies between pages can be passed in as a prop. Event handlers and state must also be added separately if the original markup is meant to respond to user input.

Choose how React should render the component

The correct rendering API depends on what is already on the page. React distinguishes between creating a client-rendered root and attaching to markup React rendered on the server. Its Client React DOM APIs reference documents both.

Page situation Use What it does
A client-rendered React area createRoot(domNode) Creates a React root in the selected DOM node so you can render a React element there.
HTML previously rendered by React on the server hydrateRoot(domNode, reactNode) Attaches React to the server-generated React markup.

Do not use hydration just because a page already contains static HTML: hydrateRoot is for markup previously generated by React. For an ordinary client-rendered area, use createRoot.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add a component to an existing non-React page

You do not have to convert an entire site at once. React’s Add React to an Existing Project guide describes mounting a component at a chosen location while leaving the rest of the page in place. For example, the HTML can include <nav id="navigation"></nav>; JavaScript can select that element, create a root there, and render a navigation component. The guide presents Vite as one option when a project lacks a JavaScript module setup; the right integration depends on the existing site.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle raw HTML carefully

Most converted markup should be written directly as JSX. Avoid inserting arbitrary HTML strings with dangerouslySetInnerHTML: React warns that untrusted markup can run attacker-controlled code. If a feature requires rendering an HTML string, use only trusted or appropriately sanitized content; sites that enforce Trusted Types should apply a suitable Trusted Types policy. See React’s common DOM components reference.

Check the conversion before using it

  • Does the component return one enclosing element?
  • Are void elements explicitly closed?
  • Have HTML attributes been adjusted to JSX property names?
  • Does the component need props, state, or event handlers that the original static markup did not contain?
  • Is any HTML string being inserted at a raw-HTML security boundary?
  • Are you using createRoot for client-rendered content or hydrateRoot only for React-generated server markup?

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.