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
  • JSX Gotcha - Use className
  • Review Challenge
  1. Part 4: JSX Challenge

4.1: JSX Overview

Before we talk about class components, it's extremely important that we discuss JSX. It might surprise you to know that we have barely written any HTML at this point. In fact, the only HTML in our app is in the index.html. The rest is called 'JSX', which stands for JavaScript XML.

Let's start by looking at a translated snippet of JSX code.

  1. Copy the following code.

import React from 'react';

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>
            </div>
        </div>
    );
}
  1. Go to the website babeljs.io.

  2. Click 'Try it out' in the navbar.

  3. Paste the code in the left box.

  4. After pasting it, you'll notice, that on the right side, you'll see an enormous chunk of code:

import React from 'react';

const JSXRules = () => {
    return React.createElement(
        'div',
        { className: 'main' },
        React.createElement(
            'div',
            { className: 'mainDiv' },
            React.createElement(
                'h1',
                { className: 'section-title' },
                'JSX'
            ),
            React.createElement(
                'dl',
                null,
                React.createElement(
                    'dt',
                    null,
                    'Resembles HTML'
                ),

            /*********************
                CODE OMITTED
            *********************/

            )
        )
    );
};

It's not necessary that you understand all of the above code right now. What is important is that you recognize what's going on under the hood with all of this code that appears to be HTML. It's actually just JavaScript in the whole file. React has made it so that we can write in what appears to be HTML. Think of the implications here: we can write JS functions and HTML in the same file in an extremely fluid way. CSS can go in there, too, but we'll get to that later.

As a React developer, you still have to know HTML5, and you have to know how to write it, since JSX highly resembles HTML5. You might think that this could be a shortcut to learning vanilla JavaScript. Quite the contrary, in fact, the more vanilla JavaScript that you know, the better you'll be with React. React works in tandem with JavaScript.

JSX Gotcha - Use className

While most of JSX highly resembles HTML5 and many of the tags are exactly the same, there are some gotchas. There is a big gotcha difference that you might have already noticed:

RULE: className is used in instead of class.

Why is this? In ES6, JavaScript introduced classes. This means that class is a keyword in JavaScript, and it has an entirely different meaning. We'll actually study that next. Hence, since JSX is actually JavaScript and since class is a keyword in JavaScript, React uses className.

Even though you might read this, you will still get hit by naming it wrong from the habit of using class, just be aware of this one.

Review Challenge

Here's a quick challenge for review:

  1. In your concepts folder, add a component called called 'JSXRules'.

  2. Use the dl, dt, dd, and tags to make a table with the following notes about JSX.

    • Resembles HTML - It's not HTML. Closer to JS.

    • React.createElement() - Used to construct and update the DOM.

    • Not required - Developers don't have to use JSX.

  3. Use an Arrow Function to create the component. (Already done)

  4. Make your component navigable from the sidebar.

PreviousPart 4: JSX ChallengeNext4.1: Challenge Answer

Last updated 7 years ago

Also of note, there are some who prefer to write their components in plain JS without JSX, like the component above. For you, as you're learning, we highly recommend sticking with JSX. It will help with visualization of the component as you're writing it. It is important to know about JSX for certain rules.

developers