← Back to React, page 1

react-0017 · Easy fresher

What is the difference between React.createElement and JSX?

JSX is readable syntax, and createElement is the function call it historically compiled to. Both produce the same thing: a plain object called a React element that describes what to render. Modern build tools compile JSX with the automatic runtime (jsx and jsxs), so createElement is rarely written by hand, but it still works and is useful for dynamic element types.

Full answer

A React element is a small object with a type, props and a key. It does not create anything in the DOM. React reads it later to decide what to draw.

createElement(type, props, ...children) builds that object. The first argument is a tag name such as 'h1' or a component function. The second is an object of props, or null. The rest are children. JSX is a friendlier way to write the same call: <h1 className="title">Hello</h1> is createElement('h1', { className: 'title' }, 'Hello').

Older projects compiled JSX to createElement, which is why every file needed import React from 'react'. Since React 17 the automatic runtime compiles JSX to calls from react/jsx-runtime, so the import is no longer needed. Output is the same kind of element, and React 19 still exports createElement.

So the two are equal in power. JSX wins on readability because nesting looks like the output. createElement can be handy when the type is chosen at runtime or when a tool generates elements without a JSX compiler.

One detail: in JSX, a lowercase tag is a DOM element and an uppercase name is a component variable. To choose the tag dynamically in JSX, store it in a capitalized variable: const Tag = level === 1 ? 'h1' : 'h2'; return <Tag>...</Tag>.

Comparing the object forms in the example shows both calls give the same markup.

jsx
import { createElement } from 'react';

function Greeting({ name }) {
  return createElement('h1', { className: 'title' }, 'Hello, ', name);
}

function GreetingJsx({ name }) {
  return <h1 className="title">Hello, {name}</h1>;
}

export default function App() {
  const level = 2;
  const Tag = level === 1 ? 'h1' : 'h2';
  return (
    <>
      <Greeting name="Asha" />
      <GreetingJsx name="Asha" />
      <Tag>Dynamic heading</Tag>
    </>
  );
}

The first two components return identical markup, one written with createElement and one with JSX. Tag shows a dynamic element type in JSX. Rendered result: <h1 class="title">Hello, Asha</h1><h1 class="title">Hello, Asha</h1><h2>Dynamic heading</h2>

Key points

  • Both create React elements, plain objects describing UI.
  • JSX compiles to jsx() calls with the automatic runtime and to createElement in the older setup.
  • createElement(type, props, ...children) still works in React 19.
  • Use JSX for readability. Use createElement for dynamic types or generated code.
  • Dynamic tags in JSX need a capitalized variable.

Common mistakes

  • Believing JSX creates DOM nodes. It creates plain objects.
  • Writing a lowercase variable as a component type, so React treats it as an HTML tag.
  • Importing React in every file out of habit when the automatic runtime makes it unnecessary.
  • Passing children as an array without keys and getting a warning.

Follow-ups

  • What does the automatic JSX runtime change in the compiled output?
  • What is inside a React element object?
  • How would you render a different heading level based on a prop?

Tips

  • Say they are the same thing in two forms. That is the point of the question.
  • Mention the automatic runtime and React 17. It dates your knowledge correctly.
  • Add the dynamic tag trick to show practical use.