Compiling JSX
Loading "Compiling Jsx (π solution)"
Run locally for transcripts
π¦ Let's break down the key changes in our
script
tag:-
type="text/babel"
anddata-type="module"
:type="text/babel"
tells Babel to transpile this script, allowing us to use JSX and modern JavaScript features.data-type="module"
indicates that this script should be treated as a module after Babel transpilation. This enables us to useimport
statements.
We use both instead of justtype="module"
because browsers don't natively understand JSX. Babel needs to transform our code first, then it can be treated as a module. -
Importing React: We've changed from:
import { createElement } from '/react.js'
to:import * as React from '/react.js'
This imports all exports from React as a namespace calledReact
. It's beneficial because:- It provides access to all React APIs, not just
createElement
. - When using JSX, the transpiler will convert JSX elements to
React.createElement
calls, so we need theReact
namespace in scope.
- It provides access to all React APIs, not just
π To dive deeper into these concepts, check out these resources:
- π Babel documentation on browser usage
- π React documentation on JSX
- π MDN on JavaScript modules
π¨βπΌ Great! Now we're ready to start using JSX in our HTML file!