⚠️ This lesson is retired and might contain outdated information.

How JSX Transforms into JavaScript

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 2 years ago

A walkthrough using the babel repl to explore how JSX transforms into JS.

Click here to open the repl.

Chris Biscardi: [0:00] One of the defining features with JSX is how it compiles into JavaScript. To show how it compiles into JavaScript, we'll have to choose the compile target. In this case, we'll work with React.

[0:11] If we go on to the babel rappel, we can take our source code on the left and see what it compiles to on the right. This a little hard to read, but we'll take it into Prettier as well. After formatting the code with Prettier, we'll take the result and put it back into our editor to look at the original source and the output side-by-side.

[0:31] On the left, we can see JSX <li> elements, JSX <h1> elements, and JSX <ul> elements. On the right, we can see there are <li> elements compiled to a React.createElement function call. This createElement call takes the type of the element and object to props in any children.

[0:51] Because the createElement call can take many children, a <ul> element takes all the <li> calls as children, and we can nest this all the way up to our <div>.

[0:59] When we start looking at props like the one on our <div>, we can see that they get passed in as an object. Finally, the reason we need to import React at the top of our source file is that the compiled output references React as part of the createElement transformation.

egghead
egghead
~ 9 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today