React Suspense API 的补丁
Provides a basic polyfill for the upcoming React Suspense APIs.
v16This module is intended for understanding and experimenting with the upcoming React Suspense APIs.
Note that the actual version of Suspense that will ship with React is significantly more complicated and efficient than the version in this polyfill. It is meant solely for experimental purposes and to ease the burden of incremental upgrades.
At its core, React Suspense works by allowing an async component to throw a Promise from its render method.
This polyfill mimics React's internal support for this behavior by implementing an error boundary in the Timeout component. If the error boundary encounters a thrown Promise, it waits until that Promise resolves and then attempts to re-render its children. It also handles falling back to loading content if the Promise takes too long to resolve.
The reason this polyfill does not support React v15 is because error boundaries weren't properly supported until React v16. If you have ideas on how to add support for React v15, submit an issue and let's discuss!
Note that React will log an error to the console regarding the thrown error, but this can safely be ignored. Unfortunately, there is no way to disable this error reporting for these types of intentional use cases.
With that being said, I hope this module and accompanying demos make it easier to get up-to-speed with React Suspense.
npm install --save react-suspense-polyfillThe only difference between using this polyfill and a suspense-enabled version of React, is that you must import { Suspense } from react-suspense-polyfill instead of from React.
With this minor change, suspense demos and react-async-elements will function as expected.
…In this example, the following rendering steps will occur:
Example's render method.Suspense will get rendered which will in turn attempt to render LazyThing.LazyThing will try to load its resource from the cache but fail and throw a Promise.Suspense (actually Timeout under the hood) will catch this Promise in its error boundary componentDidCatch.Suspense starts waiting for that Promise to resolve and kicks off a 500ms timeout. Currently, the Suspense subtree is rendering nothing.Suspense will timeout and display its fallback loading content.LazyThing resource resolves.Suspense realizes it's child has resolved and again attempts to re-render its child.LazyThing component synchronously renders the previously cached Thing component.MIT © transitive-bullshit
暂无开放 Issues,或尚未同步最近议题。