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

Pragmas and babel-transform-react-jsx

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 2 years ago

Let's change the function React uses to compile JSX to using pragmas. Pragmas are used in MDX, Emotion, theme-ui, blocks-ui, and other projects to do everything from style insertion and caching to full blown visual editing of React apps.

Open the Babel repl

Chris Biscardi: [0:00] If we look at our code in the Babel rappel, we can see that our JSX elements get transformed to React.createElement. If we add a comment at the top of the file, we can change the function that's being used.

[0:12] Note how we're no longer using React.createElement and instead, we're using a custom JSX function. We can change the name of this function to be anything.

[0:23] Importantly, we have an imported a function named anything, but the resulting code won't run unless we do that. We also don't need to import React anymore, because we're not using CreateElement.

[0:34] The comment we just used was called the pragma. A pragma gives the compiler in this case, Babel, additional instructions or hints or how to compile the code. The plugin that looks for this specific pragma it's called babel-plugin-transform-react.jsx.

[0:47] If we look at the examples, we can see the pragma being used to support frameworks like React. In this case, we're using Preact.h from the Preact package, and the resulting code uses Preact.h instead of React.createElement.

[1:01] When people refer to a pragma in the context of React, they typically mean either the comment itself or the resulting function, which can be any function we define ourselves.

egghead
egghead

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