SlideShare a Scribd company logo
Why
loveI JSX
JSX is an XML-like syntax extension to JavaScript
https://facebook.github.io/jsx/
<foo></foo>
class App extends Component {
render() {
return <div>hello world</div>;
}
}
It’s not just for React UI
…but that’s probably what you’ll use it for
Jay Phelps
Senior Software Engineer |
Jay Phelps
Chief Software Architect |
https://www.thisdot.co/
Support, Training, Mentorship, and More
JSX is an XML-like syntax extension to JavaScript
*without any defined semantics
JSX is an XML-like syntax extension to JavaScript
in practice JSX is treated as a function call expression
*without any defined semantics
// calling a function
var element = createElement('div');
// calling a function
var element = createElement('div');
// also just calling a function
var element = <div></div>;
// calling a function
var element = createElement('div');
// also just calling a function
var element = <div></div>;
Why I Love JSX!
// calling a function
var element = createElement('div');
// also just calling a function
var element = <div></div>;
/* @jsx someFuncName */
/* @jsx createElement */
var element = <div />;
/* @jsx createElement */
var element = createElement('div');
JSX can be used anywhere a JS expression can be
Because they are expressions
var object = {
first: <div />,
second: <div />,
third: <div />
};
var object = {
first: <div />,
second: <div />,
third: <div />
};
var array = [<div />, <div />, <div />];
var object = {
first: <div />,
second: <div />,
third: <div />
};
var array = [<div />, <div />, <div />];
var result = doSomething(<div />, <div />);
var object = {
first: <div />,
second: <div />,
third: <div />
};
var array = [<div />, <div />, <div />];
var result = doSomething(<div />, <div />);
They really are JavaScript expressions
they can be used in very some weird places
/* @jsx h */
var result = <foo /> + <bar />;
// var result = h('foo') + h('bar');
var result = <foo /> + <bar />;
// var result = h('foo') + h('bar');
var result = <obj />[<key />];
// var obj = h('obj');
// var key = h('key');
// var result = obj[key];
var result = <foo /> + <bar />;
// var result = h('foo') + h('bar');
var result = <obj />[<key />];
// var obj = h('obj');
// var key = h('key');
// var result = obj[key];
*These aren’t valid in React
Because you’re dealing with React Elements
var result = <foo /> + <bar />;
// var result = h('foo') + h('bar');
var result = <obj />[<key />];
// var obj = h('obj');
// var key = h('key');
// var result = obj[key];
You can pass any JS values as “props”
Visually Similar to HTML Attributes
<input id="foo" />
<input value={obj.value} />
<input disabled />
<input {...props} />
JSX elements can have children
<div>hello, Jay</div>
<div>hello, {name}</div>
<div>hello, <b>{name}</b></div>
JSX Prevents Injection Attacks
Cross-site Scripting Attacks aka XSS
const value = response.potentiallyMaliciousInput;
// This is safe:
const element = <h1>{value}</h1>;
/* @jsx createElement */
function createElement(tag, props, ...children)
<div id={obj.foo} disabled>
text {obj.bar}
</div>
createElement(
"div",
{ id: obj.foo, disabled: true },
"text ",
obj.bar
);
"div",
createElement(
{ id: obj.foo, disabled: true },
"text ",
obj.bar
);
{ id: obj.foo, disabled: true },
createElement(
"div",
"text ",
obj.bar
);
"text ",
obj.bar
createElement(
"div",
{ id: obj.foo, disabled: true },
);
function createElement(tag, props, ...children)
function createElement(tag, props, ...children)...children
function createElement(tag, props, ...children) {
console.log(children);
// ["text ", obj.bar]
}
There are some conventions it uses too
Starts with lower case:
passed as a string
Used for HTML tags
<div />
// same as
createElement('div')
Starts with Upper case:
Passed as a JavaScript Identifier Expression
Used for React Components
<Foobar />
// same as
createElement(Foobar)
Contains a dot:
Passed as a JavaScript property lookup
Usually Used for React Components
<Accordion.Section />
// same as
createElement(Accordion.Section)
It’s not just for React UI
https://github.com/jamesplease/react-request
npm install -—save react-request
import { Fetch } from 'react-request';
class App extends Component {
render() {
return (
<Fetch url="/api/example">
{({ data }) => {
// Response data is ready
if (data) {
return <div>{data}</div>;
} else {
return null;
}
}}
</Fetch>
);
}
}
class App extends Component {
render() {
return (
<Fetch url="https://jsonplaceholder.typicode.com/posts/1">
{({ fetching, failed, data }) => {
if (fetching) {
return <div>Loading data...</div>;
}
if (failed) {
return <div>The request did not succeed.</div>;
}
if (data) {
return (
<div>
<div>Post Title: {data.title}</div>
</div>
);
}
return null;
}}
</Fetch>
);
}
}
https://github.com/developit/jsxobj
npm install -—save-dev jsxobj
<webpack target="web" watch>
<entry path="src/index.js" />
<plugins>
<UglifyJS opts={{
compression: true,
mangle: false
}} />
</plugins>
</webpack>
<webpack target="web" watch>
<entry path="src/index.js" />
<plugins>
<UglifyJS opts={{
compression: true,
mangle: false
}} />
</plugins>
</webpack>
{
"name": "webpack",
"target": "web",
"watch": true,
"entry": {
"path": "src/index.js"
},
"plugins": {
"uglify-js": {
"opts": {
"compression": true,
"mangle": false
}
}
}
}
Why not ES6 Template Literals?
return jsx`
<${Box}>
${shouldShowAnswer(user)
? jsx`<${Answer} value=${false}>no</${Answer}>`
: jsx`<${Box.Comment}>Text Content</${Box.Comment}>`}
</${Box}>
`;
Template Literals have a lot of noise
return jsx`
<${Box}>
${shouldShowAnswer(user)
? jsx`<${Answer} value=${false}>no</${Answer}>`
: jsx`<${Box.Comment}>Text Content</${Box.Comment}>`}
</${Box}>
`;
return (
<Box>
{shouldShowAnswer(user)
? <Answer value={false}>no</Answer>
: <Box.Comment>Text Content</Box.Comment>}
</Box>
);
How is this different from a
templating language?
Like {{ mustache }} templates, Angular, Ember, etc
Utilize and Grow your JavaScript skills
e.g. Learning Functional Programming
var items = [1, 2, 3];
var items = [1, 2, 3];
var items = [1, 2, 3];
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
<ol>
<ol>
{{#items}}
<li>{{.}}</li>
{{/items}}
</ol>
Mustache
<ol>
{items.map(item =>
<li>{item}</li>
)}
</ol>
React
It’s Just JavaScript ™
const els = items.map(item => <li>{item}</li>);
<ul>
{doSomething(els)}
</ul>
This is why JSX is so Wonderful to use
You no longer run into the rough edges of a templating language
you will fall in love with it
Give it a chance, plz. K thnx
Experiment with other uses for JSX
Remember, JSX does not have to be react-specific
I love JSX Because It’s Just JavaScript!
@_jayphelps
The End

More Related Content

PPTX
Routing And Navigation
PDF
Stencil the time for vanilla web components has arrived
PDF
SilverStripe CMS JavaScript Refactoring
PDF
Easy tests with Selenide and Easyb
PPTX
Webinar: AngularJS and the WordPress REST API
PDF
Kiss PageObjects [01-2017]
ODP
Getting to Grips with SilverStripe Testing
PDF
Polyglot automation - QA Fest - 2015
Routing And Navigation
Stencil the time for vanilla web components has arrived
SilverStripe CMS JavaScript Refactoring
Easy tests with Selenide and Easyb
Webinar: AngularJS and the WordPress REST API
Kiss PageObjects [01-2017]
Getting to Grips with SilverStripe Testing
Polyglot automation - QA Fest - 2015

What's hot (20)

KEY
Single Page Web Apps with Backbone.js and Rails
PDF
Exploring Angular 2 - Episode 2
PPTX
Angular 2.0 Pipes
PDF
Selenide alternative in Python - Introducing Selene [SeleniumCamp 2016]
PDF
Angular 2 introduction
PDF
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
PDF
Angular 2.0 - What to expect
PDF
Advanced redux
PDF
Redux vs Alt
PPTX
Http Communication in Angular 2.0
PDF
Web ui tests examples with selenide, nselene, selene & capybara
PDF
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
PPTX
Upgrading from Angular 1.x to Angular 2.x
PDF
Unit Testing for Great Justice
ODP
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
PDF
Keeping the frontend under control with Symfony and Webpack
PDF
Nicolas Embleton, Advanced Angular JS
PDF
React on es6+
PDF
jQuery Presentation to Rails Developers
PDF
Stencil: The Time for Vanilla Web Components has Arrived
Single Page Web Apps with Backbone.js and Rails
Exploring Angular 2 - Episode 2
Angular 2.0 Pipes
Selenide alternative in Python - Introducing Selene [SeleniumCamp 2016]
Angular 2 introduction
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Angular 2.0 - What to expect
Advanced redux
Redux vs Alt
Http Communication in Angular 2.0
Web ui tests examples with selenide, nselene, selene & capybara
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Upgrading from Angular 1.x to Angular 2.x
Unit Testing for Great Justice
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Keeping the frontend under control with Symfony and Webpack
Nicolas Embleton, Advanced Angular JS
React on es6+
jQuery Presentation to Rails Developers
Stencil: The Time for Vanilla Web Components has Arrived
Ad

Similar to Why I Love JSX! (20)

PDF
The Beauty Of Java Script V5a
PDF
The Beauty of Java Script
PPTX
Getting the Most Out of jQuery Widgets
PDF
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
PDF
jQuery: out with the old, in with the new
ODP
Introduction to jQuery
PDF
JavaScript Promise
PDF
ES6: The Awesome Parts
PDF
Avinash Kundaliya: Javascript and WordPress
PPTX
ReactJs presentation
PDF
OttawaJS - React
PDF
Functional Programming with JavaScript
PPT
JSF Custom Components
PPTX
Beyond DOMReady: Ultra High-Performance Javascript
PDF
Frontin like-a-backer
KEY
How and why i roll my own node.js framework
PDF
YUI on the go
PDF
Laravel 101
KEY
BlackBerry DevCon 2011 - PhoneGap and WebWorks
PPT
The Beauty Of Java Script V5a
The Beauty of Java Script
Getting the Most Out of jQuery Widgets
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
jQuery: out with the old, in with the new
Introduction to jQuery
JavaScript Promise
ES6: The Awesome Parts
Avinash Kundaliya: Javascript and WordPress
ReactJs presentation
OttawaJS - React
Functional Programming with JavaScript
JSF Custom Components
Beyond DOMReady: Ultra High-Performance Javascript
Frontin like-a-backer
How and why i roll my own node.js framework
YUI on the go
Laravel 101
BlackBerry DevCon 2011 - PhoneGap and WebWorks
Ad

More from Jay Phelps (12)

PDF
Backpressure? Resistance is not futile. RxJS Live 2019
PDF
Backpressure? Resistance is not futile. (Uphill Conf 2019)
PDF
React, Powered by WebAssembly
PDF
The WebAssembly Revolution Has Begun
PDF
WebAssembly Demystified
PDF
Real-time Insights, powered by Reactive Programming
PDF
RxJS + Redux + React = Amazing
PDF
ES2015 and Beyond
PDF
Intro to Ember CLI
PDF
Ember Overview in 5 Minutes
PDF
Profit From Your Media Library Using Multi-Platform Distribution
PDF
Intro to Ember.js
Backpressure? Resistance is not futile. RxJS Live 2019
Backpressure? Resistance is not futile. (Uphill Conf 2019)
React, Powered by WebAssembly
The WebAssembly Revolution Has Begun
WebAssembly Demystified
Real-time Insights, powered by Reactive Programming
RxJS + Redux + React = Amazing
ES2015 and Beyond
Intro to Ember CLI
Ember Overview in 5 Minutes
Profit From Your Media Library Using Multi-Platform Distribution
Intro to Ember.js

Recently uploaded (20)

PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPT
Introduction Database Management System for Course Database
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Introduction to Artificial Intelligence
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
AI in Product Development-omnex systems
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Transform Your Business with a Software ERP System
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Introduction Database Management System for Course Database
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Introduction to Artificial Intelligence
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Online Work Permit System for Fast Permit Processing
ISO 45001 Occupational Health and Safety Management System
Design an Analysis of Algorithms I-SECS-1021-03
AI in Product Development-omnex systems
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Transform Your Business with a Software ERP System
Softaken Excel to vCard Converter Software.pdf
Operating system designcfffgfgggggggvggggggggg

Why I Love JSX!