react中解决异步
介绍 (Introduction)
Traditionally, dealing with asynchronous code in React has required some boilerplate:
传统上,在React中处理异步代码需要一些样板:
Make a wrapper component to show a pending or rejected state.
使包装器组件显示挂起或拒绝状态。
Make our components keeping track of pending and rejected state and inform the wrapper component about state changes.
让我们的组件跟踪挂起和拒绝的状态,并通知包装器组件状态更改。
But ideally, we want:
但理想情况下,我们希望:
To care only about the fulfilled state in our components.
只关心组件中的实现状态。
To abstract away the pending and rejected state.
提取挂起和拒绝状态。
Here, I am going to show you how to achieve just that and how to structure asynchronous code in a modern way.
在这里,我将向您展示如何实现这一目标以及如何以现代方式构造异步代码。
工具类 (Tools)
We are going to use the Suspense component and Error Boundaries. They have been available since React 16.6.
我们将使用Suspense组件和Error Boundaries。 从Reac