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

推荐订阅源

D
DataBreaches.Net
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Webroot Blog
Webroot Blog
AI
AI
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
T
Tor Project blog
罗磊的独立博客
小众软件
小众软件
S
Security Affairs
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
NISL@THU
NISL@THU
博客园_首页
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
A
About on SuperTechFans
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
G
GRAHAM CLULEY
雷峰网
雷峰网
F
Full Disclosure
I
Intezer
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
U
Unit 42

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 add testing to an existing project Profile a React App for Performance
How to Enable React Concurrent Mode
2019-11-04 · via Kent C. Dodds Blog

Watch "Enable React Concurrent Mode" on egghead.io (part of my Use Suspense to Simplify Your Async UI course)

React's new Concurrent Mode has just been published in the experimental release channel. It's the result of years of research and that shows. If you'd like to learn more about why it's so cool, definitely watch Dan Abramov's talk at JSIceland. And people have started playing around with it and seeing some nice perf wins out of the box.

All that said, please remember that this is experimental. The experimental release channel does not honor semver (so code relying on it could break unexpectedly) and there could definitely be bugs. But early experimentation has been promising for many and I suggest that you try it out in your own app.

Step 1

Get it installed.

First, to enable Concurrent Mode, you'll need to have a version of React that supports this. At the time of this writing, React and React DOM are at version 16.11.0 which does not support Concurrent Mode. So we'll need to install the experimental version:

npm install react@experimental react-dom@experimental
# or: yarn add react@experimental react-dom@experimental

Step 2

Make sure your app works without changing anything else.

Run your app, run your build, run your tests/type checking. If there are new errors in the console that weren't there before, then those might be bugs in React and you should try to make a minimal reproduction (preferably in a codesandbox) and open a new issue on the React repo.

Often we skip this step, but I think it's important to make sure that if there are problems you know which step these problems started at! Good advice in general I'd say 😉

Step 3

Enable Concurrent Mode.

In the entry file of your project, you probably have something that looks like this:

import * as React from 'react'
import ReactDOM from 'react-dom'
import App from './app'

const rootEl = document.getElementById('root')
ReactDOM.render(<App />, rootEl)

To enable Concurrent Mode, you'll use a new createRoot API (NOTE the unstable_ prefix):

import * as React from 'react'
import ReactDOM from 'react-dom'
import App from './app'

const rootEl = document.getElementById('root')
// ReactDOM.render(<App />, rootEl)
const root = ReactDOM.unstable_createRoot(rootEl)
root.render(<App />)

That's it.

Step 4

Make sure your app works without changing anything else.

Run your app, run your build, run your tests/type checking. If there are new errors in the console that weren't there before, then those might be bugs in React and you should try to make a minimal reproduction (preferably in a codesandbox) and open a new issue on the React repo.

If that looks familiar, it's because I copy/pasted it from step 2 😂

In this case however, if things are broken or you have new errors in the console. It may be because there's code in your app that's using features not supported in Concurrent Mode (like String Refs, Legacy Context, or findDOMNode).

Also please note that all the lifecycle methods that have the unsafe_ prefix are now actually unsafe and you will experience bugs using those.

Step 5

Try out Concurrent Mode. There are two primary things that Concurrent Mode enables for us:

  1. Time Slicing
  2. Suspense for everything asynchronous

If you have some user interaction in your app that you know is slow, try that out and if it's less janky, that's time slicing at work (watch Dan's talk linked above for more about this).

You can try refactoring one of your asynchronous interactions to suspense, or just try adding this to somewhere in your app:

function SuspenseDemo() {
	const [greetingResource, setGreetingResource] = React.useState(null)
	const [startTransition, isPending] = React.unstable_useTransition()

	function handleSubmit(event) {
		event.preventDefault()
		const name = event.target.elements.nameInput.value
		startTransition(() => {
			setGreetingResource(createGreetingResource(name))
		})
	}

	return (
		<div>
			<strong>Suspense Demo</strong>
			<form onSubmit={handleSubmit}>
				<label htmlFor="nameInput">Name</label>
				<input id="nameInput" />
				<button type="submit">Submit</button>
			</form>
			<ErrorBoundary>
				<React.Suspense fallback={<p>loading greeting</p>}>
					<Greeting greetingResource={greetingResource} isPending={isPending} />
				</React.Suspense>
			</ErrorBoundary>
		</div>
	)
}

function Greeting({ greetingResource, isPending }) {
	return (
		<p style={{ opacity: isPending ? 0.4 : 1 }}>
			{greetingResource ? greetingResource.read() : 'Please submit a name'}
		</p>
	)
}

// 🐨 make this function do something else. Like an HTTP request or something
function getGreeting(name) {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			resolve(`Hello ${name}!`)
			// 🐨 try rejecting instead... (make sure to comment out the resolve call)
			// reject(new Error(`Oh no. Could not load greeting for ${name}`))
		}, 1500) // 🐨 play with this number a bit
	})
}

// 🚨 This should NOT be copy/pasted for production code and is only here
// for experimentation purposes. The API for suspense (currently throwing a
// promise) is likely to change before suspense is officially released.
function createGreetingResource(name) {
	let status = 'pending'
	let result
	let suspender = getGreeting(name).then(
		(greeting) => {
			status = 'success'
			result = greeting
		},
		(error) => {
			status = 'error'
			result = error
		},
	)
	return {
		read() {
			if (status === 'pending') throw suspender
			if (status === 'error') throw result
			if (status === 'success') return result
		},
	}
}

class ErrorBoundary extends React.Component {
	state = { error: null }
	static getDerivedStateFromError(error) {
		return { error }
	}
	componentDidCatch() {
		// log the error to the server
	}
	tryAgain = () => this.setState({ error: null })
	render() {
		return this.state.error ? (
			<div>
				There was an error. <button onClick={this.tryAgain}>try again</button>
				<pre style={{ whiteSpace: 'normal' }}>{this.state.error.message}</pre>
			</div>
		) : (
			this.props.children
		)
	}
}

Play with this on codesandbox instead

One thing that I've found is that the suspense APIs are pretty low-level, so there's a lot of code needed to make it work well. But the cool thing is that these are atomic features which work really well within an abstraction and can be easily shared. So once you've got that abstraction, you're golden. It's awesome.

Step 6

Undo all your changes.

Reinstall the last stable version you had installed before, and restore the old ReactDOM.render you had before. Concurrent Mode is experimental, and even if it doesn't look like there are problems, shipping experimental software as foundational as React is ill-advised. The React docs even suggest that depending on the size of your app and the third party libraries you're using, you may never be able to ship Concurrent Mode (Facebook currently has no plans to enable Concurrent Mode on the old Facebook.com).

Remember also that we as a community are just starting to play around with this stuff, so there are still a lot of unknowns around trade-offs for different approaches. It's an exciting time. But if you value stability, then maybe pretend Concurrent Mode and suspense don't exist for a little while.

Step 7

Enable Strict Mode.

Apps that don't pass Strict Mode are unlikely to work well in Concurrent Mode. So if you want to work toward enabling Concurrent Mode on your app, then enable Strict Mode. One nice thing about Strict Mode is (unlike Concurrent Mode) it's incrementally adoptable. So you can apply Strict Mode to only the part of your codebase that you know is compliant and then iterate to full support over time.

Read more about this on my blog: How to Enable React Strict Mode.

Conclusion

I'm really looking forward to the stable release of Concurrent Mode and Suspense for data fetching. It's going to be even cooler when frameworks and libraries take advantage of these new features. As awesome as React Hooks were for the React ecosystem, I think that Concurrent Mode will be more impactful for both developer experience and the end user.

Enjoy experimenting!