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

推荐订阅源

人人都是产品经理
人人都是产品经理
D
Docker
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - 司徒正美
博客园 - Franky
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Secure Thoughts
博客园 - 聂微东
L
LINUX DO - 最新话题
U
Unit 42
SecWiki News
SecWiki News
A
Arctic Wolf
Schneier on Security
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Visual Studio Blog
量子位
The Cloudflare Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
Last Week in AI
Last Week in AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
Latest news
Latest news

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 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 How to add testing to an existing project Profile a React App for Performance
Super Simple Start to React
2020-04-24 · via Kent C. Dodds Blog
Click here to reveal the final version
<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script src="https://unpkg.com/react-dom@16.13.1/umd/react-dom.development.js"></script>
		<script src="https://unpkg.com/@babel/standalone@7.8.3/babel.js"></script>
		<script type="text/babel">
			ReactDOM.render(<div>Hello World</div>, document.getElementById('root'))
		</script>
	</body>
</html>

Read on to follow the step-by-step process for how we get here (and enjoy the videos throughout).


When you're learning something new (or you want to solidify your foundational understanding of something you're already familiar with), one of the most valuable things you can do is remove everything until all you're left with is the one thing you're trying to learn.

When we're talking about building applications, we're putting together many different abstractions (tools and libraries) to do so. When all you want to do is ship, it's natural to see all of those things as one big ball of rubber bands where you don't know when one abstraction starts and the other ends and it honestly doesn't really matter all that much because all you care about is getting something shipped.

Ball of Rubber bands

But if you really want to get a solid foundation and use the abstractions to their greatest potential, then you'll find you're much more effective by taking those rubber bands apart and exploring them in isolation. You'll get to know their capabilities and what role they play in the overall application. That way, when you use them in the future, you won't try to put two pieces together in a way they weren't intended, because you'll understand what their intended use cases are.

So let's go ahead and try this with React. When we build a React application, we use a ton of tools together (both development tools as well as libraries we ship to production). If you don't know where react ends and webpack starts you won't be as effective as using either. So, let's strip everything away and make it as simple as possible: a straight-up index.html file.

The next little bit will basically be a simple version of what you can watch for free in my Beginner's Guide to React course on egghead. For this next part, you can watch "Create a User Interface with Vanilla JavaScript and DOM" on egghead.io

Let's start with a regular HTML file:

<html>
	<body></body>
</html>

(Technically, you don't even need that much because the browser is very forgiving when it comes to this kind of thing and it'll add the html and body tags for you automatically. But let's keep those in.)

Alright, we're going to create DOM nodes using JavaScript and put them into a container or "root" DOM node. So let's add that:

<html>
	<body>
		<div id="root"></div>
	</body>
</html>

We give it the id of root to make it easy to find that DOM node in our JavaScript. Let's add that next:

<html>
	<body>
		<div id="root"></div>
		<script type="module">
			const rootElement = document.getElementById('root')
		</script>
	</body>
</html>

Great, now that we have the rootElement, let's create a DOM element to put inside it:

<html>
	<body>
		<div id="root"></div>
		<script type="module">
			const rootElement = document.getElementById('root')
			const element = document.createElement('div')
			element.textContent = 'Hello World'
			element.className = 'container'
			rootElement.append(element)
		</script>
	</body>
</html>

Now what you'll see on the page is "Hello World" that's rendered inside a div within our root.

Let's add React

For this next part, you can watch "Create a User Interface with React's createElement API" on egghead.io

Alright, let's add React to the page. It's a third party library, with JavaScript of its own, so we need to add separate script tags to the page for the browser to load that JavaScript for us:

<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script type="module">
			const rootElement = document.getElementById('root')
			const element = document.createElement('div')
			element.textContent = 'Hello World'
			element.className = 'container'
			rootElement.append(element)
		</script>
	</body>
</html>

Great, with React on the page (as the global variable React), we can now start creating React elements:

<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script type="module">
			const rootElement = document.getElementById('root')
			// const element = document.createElement('div')
			// element.textContent = 'Hello World'
			// element.className = 'container'
			// rootElement.append(element)

			const element = React.createElement(
				'div',
				{ className: 'container' },
				'Hello World',
			)
		</script>
	</body>
</html>

Awesome. That element is a regular JavaScript object. Go ahead and log it to the page and you'll see something like this:

{
  $$typeof: Symbol(react.element)
  key: null
  props: {className: "container", children: "Hello World"}
  ref: null
  type: "div"
  _owner: null
  _store: {validated: false}
  _self: null
  _source: null
  __proto__: Object
}

Learn more about this from my blog post What is JSX?

Now we've got to have something that can take that react element and turn it into a DOM node and then put that DOM node in our root. That's what react-dom is for. So let's add that:

<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script src="https://unpkg.com/react-dom@16.13.1/umd/react-dom.development.js"></script>
		<script type="module">
			const rootElement = document.getElementById('root')
			const element = React.createElement(
				'div',
				{ className: 'container' },
				'Hello World',
			)
			ReactDOM.render(element, rootElement)
		</script>
	</body>
</html>

Now we'll have the same thing rendered as we had with our original vanilla JavaScript solution.

Adding JSX

For this next part, you can watch "Create a User Interface with React's JSX syntax" on egghead.io

Nobody writes React like we have above though. We're all using JSX! But the browser doesn't know what JSX is! So while we like writing our code using this special syntax, we need to provide the browser with something it understands. The browser understands React.createElement. So what if we write our code using JSX, and then we have some tool that converts JSX to React.createElement? That's precisely what the Babel compiler does for us.

As it happens, Babel is written completely in JavaScript and can actually run in the browser! So let's add it to our page:

<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script src="https://unpkg.com/react-dom@16.13.1/umd/react-dom.development.js"></script>
		<script src="https://unpkg.com/@babel/standalone@7.8.3/babel.js"></script>
		<script type="module">
			const rootElement = document.getElementById('root')
			const element = React.createElement(
				'div',
				{ className: 'container' },
				'Hello World',
			)
			ReactDOM.render(element, rootElement)
		</script>
	</body>
</html>

And with that, now we can tell babel that we want it to compile the code we have in the script tag. We do this by changing the type to text/babel:

<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script src="https://unpkg.com/react-dom@16.13.1/umd/react-dom.development.js"></script>
		<script src="https://unpkg.com/@babel/standalone@7.8.3/babel.js"></script>
		<script type="text/babel">
			const rootElement = document.getElementById('root')
			const element = React.createElement(
				'div',
				{ className: 'container' },
				'Hello World',
			)
			ReactDOM.render(element, rootElement)
		</script>
	</body>
</html>

Now that we've got that set up, we can start using JSX!

<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script src="https://unpkg.com/react-dom@16.13.1/umd/react-dom.development.js"></script>
		<script src="https://unpkg.com/@babel/standalone@7.8.3/babel.js"></script>
		<script type="text/babel">
			const rootElement = document.getElementById('root')
			// const element = React.createElement(
			//   'div',
			//   {className: 'container'},
			//   'Hello World',
			// )
			const element = <div className="container">Hello World</div>
			ReactDOM.render(element, rootElement)
		</script>
	</body>
</html>

And that's it! So here's the simplified and final version of everything you need to get React running in an index.html file without any build tools whatsoever:

<html>
	<body>
		<div id="root"></div>
		<script src="https://unpkg.com/react@16.13.1/umd/react.development.js"></script>
		<script src="https://unpkg.com/react-dom@16.13.1/umd/react-dom.development.js"></script>
		<script src="https://unpkg.com/@babel/standalone@7.8.3/babel.js"></script>
		<script type="text/babel">
			ReactDOM.render(
				<div className="container">Hello World</div>,
				document.getElementById('root'),
			)
		</script>
	</body>
</html>

Conclusion

Now, I wouldn't recommend building your whole app like this, but hopefully this was instructive and helped you understand what the different parts of the React fundamental abstractions are responsible for. Taking things apart from other abstractions and adding them back one at a time can really help you understand how these tools and libraries work alone and how you can best use them together to build awesome stuff.

Rubber bands separated

If you want to continue your learning journey, don't miss The Beginner's Guide to React. Good luck!