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

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
N
News and Events Feed by Topic
H
Hacker News: Front Page
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
SecWiki News
SecWiki News
N
News | PayPal Newsroom
T
Tor Project blog
W
WeLiveSecurity
A
Arctic Wolf
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
月光博客
月光博客
AWS News Blog
AWS News Blog
D
Docker
C
CERT Recently Published Vulnerability Notes
MyScale Blog
MyScale Blog
Google Online Security Blog
Google Online Security Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
I
InfoQ
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Recorded Future
Recorded Future
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
D
DataBreaches.Net
S
Security Affairs
WordPress大学
WordPress大学
T
Threatpost
Microsoft Security Blog
Microsoft Security Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
B
Blog RSS Feed
Project Zero
Project Zero
P
Proofpoint News Feed

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
What is JSX?
2018-07-09 · via Kent C. Dodds Blog

The first few videos of the (free) Beginner's Guide to ReactJS are all about React elements and JSX. So here they are for you to watch along with this blog post. Enjoy!

Now on to the blog post!

I think a critical part of understanding how to use React effectively is understanding JavaScript and JavaScript expressions. So I'm going to show you a few examples of JSX and it's compiled version to help give you an idea of how this all works. As soon as you can compile JSX in your head, you can use the abstraction more powerfully.

Here's our simplest example:

Note, all examples assign to a variable ui just to illustrate that these are regular JavaScript expressions that you can assign to a variable.

ui = <div id="root">Hello world</div>
ui = React.createElement('div', { id: 'root' }, 'Hello world')

As shown above, the JSX is compiled to React.createElement. The API to React.createElement is:

function createElement(elementType, props, ...children) {}
  • elementType can be a string or a function (class) for the type of element to be created
  • props is an object for the props we want applied to the element (or null if we specify no props)
  • ...children is all the children we want applied to the element too. This is just a convenience and we could write an equivalent to above with:
ui = React.createElement('div', { id: 'root', children: 'Hello world' })

If you have more than one child then you use an array:

ui = (
	<div>
		<span>Hello</span> <span>World</span>
	</div>
)
ui = React.createElement('div', {
	children: [
		React.createElement('span', null, 'Hello'),
		' ',
		React.createElement('span', null, 'World'),
	],
})

// Note: babel uses the third argument for children:
ui = React.createElement(
	'div', // type
	null, // props
	// children are the rest:
	React.createElement('span', null, 'Hello'),
	' ',
	React.createElement('span', null, 'World'),
)

What you get back from a React.createElement call is actually a simple object:

// <div id="root">Hello world</div>
{
  type: "div",
  key: null,
  ref: null,
  props: { id: "root", children: "Hello world" },
  _owner: null,
  _store: {}
};

When you pass an object like that to ReactDOM.render or any other renderer, it's the renderer's job to interpret that element object and create DOM nodes or whatever else out of it. Neat right?!

Here are a few more examples for you:

ui = <div>Hello {subject}</div>
ui = React.createElement('div', null, 'Hello ', subject)

ui = (
	<div>
		{greeting} {subject}
	</div>
)
ui = React.createElement('div', null, greeting, ' ', subject)

ui = <button onClick={() => {}}>click me</button>
ui = React.createElement('button', { onClick: () => {} }, 'click me')

ui = <div>{error ? <span>{error}</span> : <span>good to go</span>}</div>
ui = React.createElement(
	'div',
	null,
	error
		? React.createElement('span', null, error)
		: React.createElement('span', null, 'good to go'),
)

ui = (
	<div>
		{items.map((i) => (
			<span key={i.id}>{i.content}</span>
		))}
	</div>
)
ui = React.createElement(
	'div',
	null,
	items.map((i) => React.createElement('span', { key: i.id }, i.content)),
)

Notice that whatever you put inside { and } is left alone. This is called an interpolation and allows you to dynamically inject variables into the values of props and children. Because of the way this works, the contents of an interpolation must be JavaScript expressions because they're essentially the right hand of an object assignment or used as an argument to a function call.

Conclusion

If you'd like to play around with this some more, you can try online with Babel's online REPL. Start here. Hopefully this helps you understand a little more about how JSX works and how you can use it more effectively. Good luck!