JS-201-ReactFundamentals
  • Part 0: App Overview
  • Part 1: Intro to React
    • 1.0: Packages Setup
    • 1.1: Project Setup
    • 1.2: React Router Dom
  • Part 2: JavaScript Concepts
    • 2.0: ES6 Overview
    • 2.1: classes
    • 2.2: constructors
    • 2.3: extends
    • 2.4: super
    • 2.5: interpolation
    • 2.6: map
    • 2.7: filter
  • Part 3: Functional Components
    • 3.0: Functional Components Overview
    • 3.1: Calling Functional Components
    • 3.2: Single Element Rule
    • 3.3: Arrow Functions
    • 3.4: Challenge
    • 3.4: Solution
    • 3.5: Challenge 2
    • 3.5: Solution 2
  • Part 4: JSX Challenge
    • 4.1: JSX Overview
    • 4.1: Challenge Answer
    • 4.2: className
  • Part 5: Class Concepts
    • 5.1: State
    • 5.2: setState
    • 5.3: Class Components Challenge
    • 5.4: Class Components Challenge Answer
  • Part 6: Props Starter
    • 6.0: Props Overview
    • 6.1: Props Demo and Challenge 1
    • 6.2: Props Answer 2
    • 6.3: Props Passing Functions and Challenge 2
    • 6.4: Props Answer 2
    • 6.5: External Props and Mapping Components
    • 6.6: PropTypes
  • Part 7: Lifecycle Methods
    • 7.0: Lifecycle Overview
    • 7.1: Lifecycle Methods
    • 7.2: Mounting Methods
    • 7.3: Update Methods
    • 7.4: Unmount Methods
  • Part 8: Apps
    • 1.0 - Small Timer Apps
      • 1.1 - Simple Timer
      • 1.2 - Clock App
      • 1.3 - Stop Watch App
      • 1.4 - Challenge
    • 2.0 - Concept List
      • 2.1 - Concept Data
      • 2.2 - Concept App Component
      • 2.3 - Concept Component
      • 2.4 - Concept List Component
    • 3.0 - NYT
      • 3.1 - NytApp Component
      • 3.2 - Nyt Results
    • 4.0 - The Friend List App
      • 4.1 - Friend List App Component
      • 4.2 - Friend Component
    • 5.0 - Movie Search Application
      • 5.1 - Form Component
      • 5.2 - Form Results Component
      • 5.3 - Styled Components
    • 6.0 - YouTube Api
      • 6.1 - Video Component
      • 6.2 - Video Component
      • 6.3 - Video Component
    • 7.0 - Github Api Application
      • 7.1 - The Users
      • 7.2 - Github API Component
      • 7.3 - Github API Card
      • 7.4 - Github API Card Form
      • 7.5 - Github API Card List
      • 7.6 - Github API Search
      • 7.7 - Github API Challenge
    • 8.0 - Bitcoin Api Application
      • 8.1 - Bitcoin API Setup
      • 8.2 - Bitcoin API Fetch
      • 8.3 - Bitcoin API Line Chart
      • 8.4 - Bitcoin API Fetching Data
      • 8.5 - Bitcoin API Info Box
      • 8.6 - Bitcoin API Completed Code
    • 9.0 - Google Maps Api Challenge
    • 10.0 - Sound Cloud App Challenge
    • 11.0 - VR App Challenge
    • 12.0 - React Native Intro
  • Part 9: Project
  • Part 10: Notes
    • 10.1 - Resources
    • 10.2 - Troubleshooting
Powered by GitBook
On this page
  • Getting started
  • Add Babel & Behave
  • Call the Component
  • Add some starter CSS
  • Babel.js
  1. Part 4: JSX Challenge

4.2: className

Previous4.1: Challenge AnswerNextPart 5: Class Concepts

Last updated 7 years ago

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.

Getting started

Here's what you need to do to add your built in babeljs.io-style compiler:

  1. In your components folder, create a file called JSXCompiler.js.

  2. 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.

  3. Either way, do more than just copy and paste here.

  4. 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:

// Source: https://jsbin.com/qonaga/edit?js,output

import React from 'react';

class JSXCompiler extends React.Component {
    constructor() {
        super();
        this.state = {
            input: '/* add your jsx here */',
            output: '',
            err: ''
        }
    }
    update(e) {
        let code = e.target.value;
        try {
            this.setState({
                output: window.Babel
                    .transform(code, { presets: ['es2015', 'react'] })
                    .code,
                err: ''
            })
        }
        catch (err) {
            this.setState({ err: err.message })
        }
    }
    render() {
        return (
            <div>
                <header className="compiler-header">{this.state.err}</header>
                <div className="compiler-container">
                    <pre>
                        {this.state.output}
                    </pre>
                    <textarea
                        onChange={this.update.bind(this)}
                        defaultValue={this.state.input} />
                </div>
            </div>
        )
    }
}

export default JSXCompiler;

Add Babel & Behave

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:

    <!--Previous code omitted for brevity-->
    <div id="root"></div>

    <!--These additions must go under the "root"-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.1/babel.min.js"></script>
    <script src="index.js"></script>
    <script src="//cdn.jsdelivr.net/behave.js/0.1/behave.js"></script>
    <script>
    var editor = new Behave({
      textarea:document.querySelector('textarea'),
      replaceTab:     true,
      softTabs:       true,
      tabSize:        2,
      autoOpen:       true,
      overwrite:      true,
      autoStrip:      true,
      autoIndent:     true
    });
    </script>

Call the Component

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]

    import React from 'react';
+   import JSXCompiler from '../JSXCompiler'

    const JSXRules = () => {
        return(
            <div className="main">
                <div className="mainDiv">
                    <h1 className="section-title">JSX</h1> 
                    <dl>
                        <dt>Resembles HTML</dt>
                        <dd>It's not. It's actually closer to JavaScript.</dd>
                        <dt>"React elements"</dt>
                        <dd>These are used to construct and update the DOM, or what you see on the screen.</dd>
                        <dt>Not required</dt>
                        <dd>You can write in Vanilla JS, but most sane people use JSX.</dd>
                    </dl>
+                   <hr />
+                   <h1>Egghead.io's JSX Compiler</h1>
+                   <JSXCompiler />
                </div>
            </div>
        );
    }

    export default JSXRules;

You can now run the app. It should look like this:

Add some starter CSS

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.

/****************
COMPILER
****************/
  .compiler-header {
    display: block;
    height: 5vh;
    overflow: auto;
    background-color: yellow;
    color: red;
    font-size: 28px
  }

  .compiler-container {
    height: 95vh;
    display:flex;
  }

  pre {
    background-color: #f8f8f8;
  }

  pre, textarea {
    height: 95vh;
    width: 50%;
    font-family: monospace;
    font-size: 18px;
    margin: 0;
    padding: 10px;
    color: #222;
  }

  textarea:focus {outline: 0;}

Babel.js

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.

Egghead.io
Spoiler
Here
JSX-Compiler