My Docs
BlogGithubLinkedin
Blog-Content
Blog-Content
  • Blog
  • README
    • 10. Regular Expression Matching
    • 13. Roman to Integer
    • 14. Longest Common Prefix
    • 14. Longest Common Prefix
    • 19. Remove Nth Node From End of List
    • 19. Remove Nth Node From End of List
    • 8. String to Integer (atoi)
    • Table of contents
    • All the Things You Can Do With GitHub API and Python
    • Archive
    • Articles
    • Bash Commands That Save Me Time and Frustration
    • Basic Web Development Environment Setup
    • Basic Web Development Environment Setup
    • Blog Archive
    • Blog Archive
    • Blog
    • Bookmarks
    • Cheatsheet:
    • Clock
    • Community
    • Constructor Functions
    • Content
    • Cool Github Profiles
    • Data Structures Interview
    • Docs
    • dynamic-time-warping
    • Embed Showcase
    • Es6 Features
    • Functions
    • Gatsby Paginate
    • Getting Started
    • Google Cloud
    • google-sheets-api
    • History API
    • How to install Python 2.7 on Ubuntu 20.04 LTS
    • HTML SPEC
    • index
    • Installation
    • Installing Node
    • Interactive
    • Intro To NodeJS
    • Intro To React
    • Intro To React
    • Introducing JSX
    • Introduction to npm
    • Javascript and Node
    • Javascript and Node
    • Javascript and Node
    • Javascript Interview Questions:
    • Javascript Interview Questions:
    • Javascript Practice
    • Javascript Practice
    • Javascript Practice
    • JS Fat Arrow Functions
    • Jupyter Notebooks
    • Jupyter Notebooks
    • Leetcode
    • Leetcode
    • Leetcode
    • lorem-ipsum
    • Lorem ipsum
    • Markdown
    • My Favorite VSCode Themes
    • Nature
    • New Conference
    • Node APIs With Express
    • Node APIs With Express
    • Node Buffers
    • Node Docs
    • Node Export Module
    • Node Export Module
    • Node Modules System
    • Node vs Browser
    • Overview
    • Phone Number
    • Process in Linux
    • Pull Requests
    • Python at length
    • Python Quiz
    • Python Quiz
    • Python Snippets
    • Python Snippets
    • React Class Components Demo
    • React Class Components Demo
    • React Docs
    • React In Depth
    • RECENT PROJECTS
    • RECENT PROJECTS
    • Reference
    • Resume
    • Search:
    • Semantic Versioning
    • Showcase
    • Showcase
    • Sorting Algorithms
    • Sorting Strings
    • Starter Theme
    • Starter Theme
    • The Node.js Event Loop
    • The Node.js Event Loop
    • Tools
    • Tools
    • Trouble Shooting
    • Typography
    • UI Components
    • Understanding PATH
    • Understanding PATH
    • URL:
    • Visualizing the Discrete Fourier Transform
    • Web Apis
    • Web Design
    • Web Dev Bookmarks
    • Web Developer Tools
    • What is THIS
    • What is THIS
    • where-is-npm-pack
    • with some basic knowledge of React
    • Zumzi Instant Messenger
Powered by GitBook
On this page
  • Why JSX?
  • Embedding Expressions in JSX
  • JSX is an Expression Too
  • Specifying Attributes with JSX
  • Specifying Children with JSX
  • JSX Prevents Injection Attacks
  • JSX Represents Objects

Was this helpful?

Edit on GitHub
  1. README

Introducing JSX

PreviousIntro To ReactNextIntroduction to npm

Last updated 3 years ago

Was this helpful?

Consider this variable declaration:

This funny tag syntax is neither a string nor HTML.

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.

JSX produces React “elements”. We will explore rendering them to the DOM in the . Below, you can find the basics of JSX necessary to get you started.

Why JSX?

React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.

Instead of artificially separating technologies by putting markup and logic in separate files, React with loosely coupled units called “components” that contain both. We will come back to components in a , but if you're not yet comfortable putting markup in JS, might convince you otherwise.

React using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code. It also allows React to show more useful error and warning messages.

With that out of the way, let's get started!

Embedding Expressions in JSX

In the example below, we declare a variable called name and then use it inside JSX by wrapping it in curly braces:

You can put any valid inside the curly braces in JSX. For example, 2 + 2, user.firstName, or formatName(user) are all valid JavaScript expressions.

In the example below, we embed the result of calling a JavaScript function, formatName(user), into an <h1> element.

We split JSX over multiple lines for readability. While it isn't required, when doing this, we also recommend wrapping it in parentheses to avoid the pitfalls of .

JSX is an Expression Too

After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.

This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:

Specifying Attributes with JSX

You may use quotes to specify string literals as attributes:

You may also use curly braces to embed a JavaScript expression in an attribute:

Don't put quotes around curly braces when embedding a JavaScript expression in an attribute. You should either use quotes (for string values) or curly braces (for expressions), but not both in the same attribute.

Warning:

Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.

Specifying Children with JSX

If a tag is empty, you may close it immediately with />, like XML:

JSX tags may contain children:

JSX Prevents Injection Attacks

It is safe to embed user input in JSX:

JSX Represents Objects

Babel compiles JSX down to React.createElement() calls.

These two examples are identical:

React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object like this:

These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date.

Tip:

For example, class becomes in JSX, and tabindex becomes .

By default, React DOM any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that's not explicitly written in your application. Everything is converted to a string before being rendered. This helps prevent attacks.

We will explore rendering React elements to the DOM in the .

We recommend using the for your editor of choice so that both ES6 and JSX code is properly highlighted.

next section
separates concerns
further section
this talk
doesn't require
JavaScript expression
Try it on CodePen
automatic semicolon insertion
className
tabIndex
escapes
XSS (cross-site-scripting)
next section
“Babel” language definition