4.2: className
Last updated
Last updated
Let's build our own JSX compiler, like the one that you just used at Babeljs.io
. Please note that this entire component is taken from one of the best React fundamental courses online: . It's totally free.
Where the last module was a review challenge, this module will have you anticipating some big topics to come, including Class Components, State, constructors, and several other concepts. You'll see code here that you won't understand until after we work through a lot more modules, but it's still fun to go play around with to get a big picture idea of the power of React. Also, as you start to write components we can have a handy built in compiler to play around with.
Here's what you need to do to add your built in babeljs.io-style compiler:
In your components folder, create a file called JSXCompiler.js
.
In that file, go ahead and add the following code. You have two choices for approaching:
Copy and paste and read through the code a few times
Type it in and read as you type.
Either way, do more than just copy and paste here.
Once you get the code added, read through it again and try to decipher what is happening. Realize that you are probably not going to understand at this point. However, get outside of your comfort zone and familiarize yourself with some of the vocabulary that you're going to see in a React application. We'll start to talk about what it means in our ClassComponent & State modules.
Here is the code for the JSXCompiler
file:
You'll need to add a few things to your index.html
file for the compiler to run. The file is in your public
directory. We'll stay out of the weeds and just say that these are scripts that help make this compilation happen. Notice that the cdn link is for babel.min.js
.
Here's the code:
The component won't run quite yet. You need to call it in the JSXRules file. Try not to look ahead. This is something you know how to do:
Try to call the component in the JSXRules component.
(Spoiler below)
[This is the spoiler text]
You can now run the app. It should look like this:
Let's style the compiler a little. As always, eventually you'll want to take your own direction with the styling. Set up a new section in your App.css
called Compiler
underneath the rest of your CSS.
Also, let us remind you that this code is originally from Egghead.io. Check out the lessons there for more information.
One paragraph doesn't seem like justice for one of the most important tools in all of this: Babel, which we've added for this component. is a great read for this enormously popular and important tool. What does Babel do for us? In the article you'll find a lot of information about how Babel transpiles ES7 and ES6 to ES5. Essentially, Babel lets you use all of the newest features of the newest versions of JavaScript (like ES7) without sacrificing backwards compatibility for older browsers. It translates new concepts for older browsers. It also has awesome support for dozens of different build & test systems. Be aware that sometimes you'll need to configure Babel & Webpack for a project, but that is outside of our current objective of learning about React.