JSX explained: React.createElement() behind the scenes

This surprised me when I first learned React: JSX is not a feature of the browser. And it’s not magic either. Every JSX element you write eventually becomes a React.createElement() call. That’s the whole relationship. JSX is just a 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗳𝗿𝗶𝗲𝗻𝗱𝗹𝘆 𝘀𝘆𝗻𝘁𝗮𝘅. React.createElement() is what React actually understands. When you write JSX, you’re optimizing 𝗿𝗲𝗮𝗱𝗮𝗯𝗶𝗹𝗶𝘁𝘆. When React runs your code, it only sees 𝗽𝗹𝗮𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗼𝗯𝗷𝗲𝗰𝘁𝘀. Understanding this changed how I debugged React apps. Errors stopped feeling 𝗺𝘆𝘀𝘁𝗲𝗿𝗶𝗼𝘂𝘀. Components felt less magical and more predictable. JSX makes React pleasant to write. createElement makes it possible to run. Different layers. Same outcome. #React #JavaScript #FrontendEngineering #SoftwareDesign

During build process JSX is transpiled Js. One of the parser is https://babeljs.io/docs/babel-plugin-syntax-jsx

React was built in an awesome way. Love how you explained this.

This is a great reminder that JSX is a developer convenience, not a browser feature. React ultimately works with React.createElement() and plain JavaScript objects. Once this mental model clicks, components stop feeling mysterious and become much more predictable, especially when debugging.

Now console.log what that function returns, it's just an object. As a react developer your JSX is just a fancy object machine. Also JSX converts to jsx() now, but same idea

Yes, it’s just a nicer way to write plain JS in the end. Knowing that helps a lot.

The hoisting part is just as useful. Before, script tags added in useEffect would pile up in random spots in the DOM if components mounted and unmounted. React 19 keeps them organized in the document head automatically.

One more interesting fact, React components must return a single root element because JSX compiles to a function call that can return only one React element.

See more comments

To view or add a comment, sign in

Explore content categories