惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Vulnerabilities – Threatpost
云风的 BLOG
云风的 BLOG
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
Scott Helme
Scott Helme
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
S
Security Affairs
宝玉的分享
宝玉的分享

oida.dev | TypeScript, Rust

TypeScript's `erasableSyntaxOnly` Flag Unsafe for work Tokio: Macros Tokio: Channels Tokio: Getting Started Network Applications on the Tokio Stack Remake, Remodel, Reduce. The `never` type and error handling in TypeScript 5 Inconvenient Truths about TypeScript Refactoring in Rust: Introducing Traits Refactoring in Rust: Abstraction with the Newtype Pattern Announcing the TypeScript Cookbook TypeScript: Iterating over objects The road to universal JavaScript 10 years of oida.dev Rust: Tiny little traits The TypeScript converging point How not to learn TypeScript Getting started with Rust Introducing Slides and Coverage TypeScript: The humble function overload TypeScript + React: Children types are broken TypeScript: In defense of any Rust: Enums to wrap multiple errors Dissecting Deno Error handling in Rust TypeScript: Unexpected intersections Upgrading Node.js dependencies after a yarn audit TypeScript: Array.includes on narrow types TypeScript + React: Typing Generic forwardRefs shared, util, core: Schroedinger's module names Learning Rust and Go TypeScript: Narrow types in catch clauses TypeScript: Low maintenance types Tidy TypeScript: Name your generics Tidy TypeScript: Avoid traditional OOP patterns Tidy TypeScript: Prefer type aliases over interfaces Tidy TypeScript: Prefer union types over enums My new book: TypeScript in 50 Lessons Go Preact! ❤️ this in JavaScript and TypeScript TypeScript and ECMAScript Modules TypeScript + React: Why I don't use React.FC TypeScript + React: Component patterns TypeScript: Augmenting global and lib.dom.d.ts Vite with Preact and TypeScript TypeScript: Union to intersection type 11ty: Generate Twitter cards automatically Are large node module dependencies an issue? TypeScript: Variadic Tuple Types Preview TypeScript: Improving Object.keys Remake, Remodel. Part 4. TypeScript + React: Typing custom hooks with tuple types TypeScript: Assertion signatures and Object.defineProperty TypeScript: Check for object properties and narrow down type Boolean in JavaScript and TypeScript void in JavaScript and TypeScript Symbols in JavaScript and TypeScript Why I use TypeScript TypeScript + React: Extending JSX Elements TypeScript: Validate mapped types and const context TypeScript: Match the exact object shape TypeScript: The constructor interface pattern Streaming your Meetup - Part 4: Directing and Streaming with OBS Streaming your Meetup - Part 3: Speaker audio Streaming your Meetup - Part 2: Speaker video Streaming your Meetup - Part 1: Basics and Projector TypeScript and React Guide: Added a new styles chapter TypeScript and React Guide: Added a new render props chapter TypeScript and React: Styles and CSS TypeScript and React TypeScript and React Guide: Added a new prop types chapter TypeScript without TypeScript -- JSDoc superpowers TypeScript: Mapped types for type maps JAMStack vs serverless web apps The Unsung Benefits of JAMStack Sites TypeScript: Ambient modules for Webpack loaders My most favourite talks in 2018 TypeScript and React Guide: Added a new context chapter TypeScript: Built-in generic types TypeScript: Type predicates TypeScript and React Guide: Added a new hooks chapter Getting your CfP application right FAQ on our Angular Connect Talk: Automating UI development TypeScript and Substitutability Debugging Node.js apps in TypeScript with Visual Studio Code From Medium: Deconfusing Pre- and Post-processing From Medium: PostCSS misconceptions Saving and scraping a website with Puppeteer Cutting the mustard - 2018 edition Wordpress as CMS for your JAMStack sites My most favourite podcast episodes in 2017 My most favourite talks in 2017 My most favourite books in 2017 The Best Request Is No Request, Revisited Not so hidden figures - Organizing ScriptConf My podcast journey to ScriptCast Grid layout, grid layout everywhere! #scriptconf and #devone Object streams in Node.js
JSX is syntactic sugar
2018-11-21 · via oida.dev | TypeScript, Rust

If you follow me you know that I’m super late to the React game. It was not until functional components showed up that I got really interested in the framework. I just loved the idea of having everything wrapped in an easy function rather than needing to navigate up and down a class to get everything together. One thing that put me off in the beginning though was JSX. And I’m sure I’m not the only one. Every time I talk to people about my newly found React love, this point comes up constantly.

“JSX mixes HTML with my JavaScript, that’s ugly!”

Except that JSX doesn’t. Here’s what JSX is not:

  • JSX is not a templating language
  • JSX is not HTML
  • JSX is not XML

JSX looks like all that, but it’s nothing but syntactic sugar.

JSX is function calls #

JSX translates into pure, nested function calls. The React method signature of JSX is (element, properties, ...children). With element being either a React component or a string, properties being a JS object with keys and values. Children being empty, or an array with more function calls.

So:

<Button onClick={() => alert('YES')}>Click me</Button>

translates to:

React.createElement(Button, { onClick: () => alert('YES') }, 'Click me');

With nested elements, it looks something like this:

This JSX

<Button onClick={() => alert('YES')}><span>Click me</span></Button>

translates to:

React.createElement(Button, { onClick: () => alert('YES') }, 
React.createElement('span', {}, 'Click me'));

What are the implications of that, especially compared to templates?

  • There’s no runtime compilation and parsing of templates. Everything goes directly to the virtual DOM or layout engine underneath. That’s why it also works with Vue.js so well.
  • There’s no expressions to evaluate. Everything around is JavaScript.
  • Every component property is translatable to a JSX object key. This allows us to type check them. TypeScript works so well with JSX because there’s JavaScript underneath.

So everything looks like XML, except that it’s JavaScript functions. If you are a seasoned web developer like I am, think like that: Ever wanted to write to the DOM directly, but gave up because it’s so unwieldy? Come on, document.createElement is probably easy, but you have to do a ton of calls to the DOM API to get what you can achieve so easily by writing HTML.

JSX solves that. With JSX you have a nice and familiar syntax of writing elements without HTML.

Writing the DOM with JSX #

I mentioned TypeScript earlier. TypeScript is a full blown JSX compiler. With TypeScript, we have the possibility to change the JSX factory. That’s how TypeScript is able to compile JSX for React, Vue.js, Dojo… any other framework using JSX in one way or the other. The virtual DOM implementations underneath might differ, but the interface is the same:

/**
* element: string or component
* properties: object or null
* ...children: null or calls to the factory
*/

function factory(element, properties, ...children) { ... }

We can use the same factory method signature not only to work with the virtual DOM, we can also use this to work with the real DOM. Just to have a nice API on top of document.createElement.

Let’s try! These are the features we want to implement:

  1. Parse JSX to DOM nodes, including attributes
  2. Have simple, functional components for more composability and flexibility.

Step 1: TypeScript needs to know how to compile JSX for us. Setting two properties in tsconfig.json is all we need.

{
"compilerOptions": {
...
"jsx": "react",
"jsxFactory": "DOMcreateElement",
}
}

We leave it to the React JSX pattern (the method signature we were talking earlier), but tell TypeScript to use our soon to be created function DOMcreateElement for that.

Next, we implement our factory function. This is just a couple lines of code, so I’ll leave everything here and have detailed comments below:

/**
* A helper function that ensures we won't work with null values
*/

function nonNull(val, fallback) { return Boolean(val) ? val : fallback };

/**
* How do we handle children. Children can either be:
* 1. Calls to DOMcreateElement, returns a Node
* 2. Text content, returns a Text
*
* Both can be appended to other nodes.
*/

function DOMparseChildren(children) {
return children.map(child => {
if(typeof child === 'string') {
return document.createTextNode(child);
}
return child;
})
}

/**
* How do we handle regular nodes.
* 1. We create an element
* 2. We apply all properties from JSX to this DOM node
* 3. If available, we append all children.
*/

function DOMparseNode(element, properties, children) {
const el = document.createElement(element);
Object.keys(nonNull(properties, {})).forEach(key => {
el[key] = properties[key];
})
DOMparseChildren(children).forEach(child => {
el.appendChild(child);
});
return el;
}

/**
* Our entry function.
* 1. Is the element a function, than it's a functional component.
* We call this function (pass props and children of course)
* and return the result. We expect a return value of type Node
* 2. If the element is a string, we parse a regular node
*/

function DOMcreateElement(element, properties, ...children) {
if(typeof element === 'function') {
return element({
...nonNull(properties, {}),
children
});
}
return DOMparseNode(element, properties, children);
}

To sum it up:

  1. The factory function takes elements. Elements can be of type string or a function.
  2. A function element is a component. We call the function, because we expect to get a DOM Node out of it. If the function component has also more function components inside, they will eventually resolve to a DOM Node at some point
  3. If the element is a string, we create a regular DOM Node. For that we call document.createElement
  4. All properties are passed to the newly created Node. Now you might understand why React has something like className instead of class. This is because the DOM API underneath is also className. onClick is camel-case, though, which I find a little odd.
  5. Our implementation only allows DOM Node properties in our JSX, because of that simple property passing
  6. If our component has children (pushed together in an array), we parse children as well and append them.
  7. Children can be either a call to DOMcreateElement, resolving in a DOM Node eventually. Or a simple string.
  8. If it’s a string, we create a Text. Texts can also be appended to a DOM Node.

That’s all there is! Look at the following code example:

const Button = ({ msg }) => {
return <button onclick={() => alert(msg)}>
<strong>Click me</strong>
</button>
}

const el = <div>
<h1 className="what">Hello world</h1>
<p>
Lorem ipsum dolor sit, amet consectetur
adipisicing elit. Quae sed consectetur
placeat veritatis
illo vitae quos aut unde doloribus, minima eveniet et
eius voluptatibus minus aperiam
sequi asperiores, odio ad?
</p>
<Button msg='Yay' />
<Button msg='Nay' />
</div>

document.body.appendChild(el);

Our JSX implementation returns a DOM Node with all its children. We can even use function components for it. Instead of templates, we work with the DOM directly. But the API is a lot nicer!

Bottom line #

JSX is syntactic sugar for function calls. This allows us to work with the DOM or virtual DOM directly, without any detours. This is also what makes JSX so powerful, even if it’s so simple: All around and inside is JavaScript. You can be as expresssive as you can be with JavaScript, you are not limited to any templating language.

This also means that JSX is just as nice and beautiful to read as the code written with it. Producing bad and unreadable code can happen to everybody in every programming language. A bit of syntactic sugar won’t help here.

For me, putting together this little example helped me a lot to understand what’s going on behind the scenes. And it made me appreciate JSX and React a lot more. Now I know that I’m not mixing HTML with JavaScript or something like that. I’m calling functions. It just has a lot of angle brackets…

P.S. You can find the code at GitHub

Related Articles